Mockito - Mockito cannot mock this class - IllegalArgumentException: Could not create type

mockito can only mock non-private & non-final classes java 8
bytebuddy
mockito illegalargumentexception
mockito cannot mock this class interface org springframework core env environment
org.mockito.exceptions.base.mockitoexception: unable to create mock instance of type
mockito cannot mock this class interface retrofit2 call
mockito maven
org.mockito.exceptions.base.mockitoexception: could not modify all classes

I was trying to use a Mock class in a test of mine and getting this error:

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class com.example.Car.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.


Java               : 1.8
JVM vendor name    : Oracle Corporation
JVM vendor version : 25.161-b12
JVM name           : Java HotSpot(TM) Client VM
JVM version        : 1.8.0_161-b12
JVM info           : mixed mode, sharing
OS name            : Windows 10
OS version         : 10.0


Underlying exception : java.lang.IllegalArgumentException: Could not create type

at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:43)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:74)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: Could not create type
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:139)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:344)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:159)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:353)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:32)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:71)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:42)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
at org.mockito.Mockito.mock(Mockito.java:1908)
at org.mockito.internal.configuration.MockAnnotationProcessor.processAnnotationForMock(MockAnnotationProcessor.java:44)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:19)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:38)
at org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:62)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:49)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:69)
... 21 more
Caused by: java.lang.NoSuchMethodError: net.bytebuddy.dynamic.loading.MultipleParentClassLoader$Builder.appendMostSpecific(Ljava/util/Collection;)Lnet/bytebuddy/dynamic/loading/MultipleParentClassLoader$Builder;
at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:83)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:37)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:34)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:137)
... 39 more

In the pom:

     <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.28.2</version>
        <scope>test</scope>
    </dependency>

(Also tried version 3.0.0 )

class Car :

public class Car{

  // Some properties. Non of them is final / static.

 // constructor
}

Test class:

@RunWith(MockitoJUnitRunner.class)
public class ParkinglotTest{

@Mock
private Car car;

private Parkinglog parkinglot;

@Before
 public void setUp(){
    parkinglot = new Parkinglot(car);
}


....

I was able to fix the problem after downgrading the Mockito version:

<version>2.13.0</version>

Unexpected error (Mockito can only mock non-private & non-final , IllegalArgumentException: Could not create type at net.bytebuddy.TypeCache. Closed. Unable to migrate from Mockito 2.23.4 to 2.24.x or 2.25.x #1671. Closed MockitoException: Mockito cannot mock this class: interface  I am not sure what we on the Mockito team can do about that. If Maven resolves to a dependency that does not satisfy the constraints we declare in our pom, that seems like an issue of Maven.

It seems that this might be caused because it can't find bytebuddy installed, referring to:

Caused by: java.lang.NoSuchMethodError: net.bytebuddy.dynamic.loading.MultipleParentClassLoader$Builder.appendMostSpecific(Ljava/util/Collection;)Lnet/bytebuddy/dynamic/loading/MultipleParentClassLoader$Builder;

From what I can find online adding bytebuddy to your dependencies might fix this.

    <dependency>
      <groupId>net.bytebuddy</groupId>
      <artifactId>byte-buddy</artifactId>
      <version>XXX</version>
    </dependency>

Unable to migrate from Mockito 2.23.4 to 2.24.x or 2.25.x · Issue , MockitoException: Mockito cannot mock this class: interfac IllegalArgumentException: Could not create type at testXXXs(xxx. org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class com.senorpez.trident.clock.PlanetaryCalendarViewModel. Mockito can only mock non-private & non-final classes. If you're not sure why you're getting this error, please report to the mailing list.

In my case, it was caused because it was referring to another version of byte-buddy which was actually not compatible with the mockito library.

This additional version of byte-buddy, coming from hibernate-core (version 5.3.7) which was also present in my classpath.

Initially, I was using mockito-core library version-3.3.3 which actually required byte-buddy 1.10.5.

So, I downgraded the mockito-core library to 2.21.0 version which uses byte-buddy version 1.8.15.

And it started working fine for me.

An additional solution for me was to update to a newer version of hibernate (version>=5.4.11.Final) which includes byte-buddy version compatible with the mockito library.

But that could have a larger impact on the project so I went for the first solution like follows depending on which library you are using :

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>2.21.0</version>
        <scope>test</scope>
    </dependency>

OR

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.21.0</version>
        <scope>test</scope>
    </dependency>

Also, this issue was being addressed here: https://github.com/mockito/mockito/issues/1606 which helped me to find the solution for me.

How to Fix A "Mockito cannot mock this class" Exception in A Spring , MockitoException: Mockito cannot mock this class: interface java.util.Iterator. java.lang.IllegalArgumentException: Could not create type  testXXXs(xxx.TestXXX) Time elapsed: 0.093 s <<< ERROR! org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: interface org.springframework.security.core.context.SecurityContext. Mockito can only mock non-private & non-final classes. If you're not sure why you're getting this error, please report to the mailing list.

Problems with Mockito - 21. Unit Testing and Audio Playback, IllegalStateException: Failed to load ApplicationContext Caused by: org.mockito.​exceptions.base. Mockito can only mock non-private & non-final classes. (​particularly in regards to the @WebMvcTest annotation) creates the application context and Type-safe Configuration Properties in Spring Boot 2. Mockito Mock static method In this post, we will see about Mockito Mock static method. Maven Dependencies. @RunWith(PowerMockRunner. Note that you can mock static methods in a class even though the class is final. Mock Android TextUtils static method. From the javadoc: Creates class mock with a specified strategy for its answers to interactions.

Developers, MockitoException: Mockito cannot mock this class: class com.bignerdranch.​android.beatbox IllegalArgumentException: Could not create type Void Return Type. In Mockito we can use different methods to call real method or mock void method. Read more → As with other articles focused on the Mockito framework (like Mockito Verify , Mockito When/Then , and Mockito's Mock Methods ) the MyList class shown below will be used as the collaborator in test cases.

[jira] [Work logged] (BEAM-1009) Upgrade to Mockito 3, Using mockito 2.7.19. Underlying exception : java.lang.IllegalArgumentException​: Could not create type at org.mockito.internal.junit. In this quick article, we showed how PowerMock could be used to extend the capability of Mockito for mocking and verification of private methods in the class under test. ; System Under Test. We used to publish every version to Maven Central but we changed this strategy based on feedback from the community ( #911 ).