mockito: Unexpected error (Mockito can only mock non-private & non-final classes.) in version 2.24.0
Hi,
Since version 2.24.0 I got the error “Mockito can only mock non-private & non-final classes.”. Reverting back to Mockito 2.23.4 resolves the problem.
Stacktrace
Underlying exception : java.lang.IllegalArgumentException: Could not create type at nl.tkp.ddw.pensioenaangifte.test.model.unittesten.AanleveringFilteringStepDefinitions.setup(AanleveringFilteringStepDefinitions.java:43) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at cucumber.runtime.Utils$1.call(Utils.java:26) at cucumber.runtime.Timeout.timeout(Timeout.java:16) at cucumber.runtime.Utils.invoke(Utils.java:20) at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60) at cucumber.runner.HookDefinitionMatch.runStep(HookDefinitionMatch.java:16) at cucumber.runner.TestStep.executeStep(TestStep.java:63) at cucumber.runner.TestStep.run(TestStep.java:49) at cucumber.runner.TestCase.run(TestCase.java:40) at cucumber.runner.Runner.runPickle(Runner.java:40) at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:146) at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68) at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23) 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 cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73) at cucumber.api.junit.Cucumber.runChild(Cucumber.java:124) at cucumber.api.junit.Cucumber.runChild(Cucumber.java:65) 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 cucumber.api.junit.Cucumber$1.evaluate(Cucumber.java:133) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38) at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62) at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) at com.sun.proxy.$Proxy2.processTestClass(Unknown Source) at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:118) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:175) at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:157) at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404) at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.IllegalArgumentException: Could not create type at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:154) at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:365) at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:174) at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:376) 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:1896) 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) ... 63 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:152) ... 81 more
Versioninfo Java: 1.8 JVM vendor name: Oracle Corporation JVM vendor version: 25.191-b12 JVM name: Java HotSpot™ 64-Bit Server VM JVM version: 1.8.0_191-b12 JVM info: mixed mode OS name: Mac OS X OS version: 10.13.6 Mockito-core: 2.24.0
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 32
- Comments: 81 (31 by maintainers)
Links to this issue
Commits related to this issue
- Bumped ByteBuddy to 1.9.11 Fixes #1606 — committed to mockito/mockito by mockitoguy 5 years ago
- Bumped ByteBuddy to 1.9.11 Fixes #1606 — committed to mockito/mockito by mockitoguy 5 years ago
- test(dashboard): Reverting mockito's version to 2.23.4 https://github.com/mockito/mockito/issues/1606 — committed to Anilople/Sentinel by Anilople 3 years ago
Since quite some people have the same issue, i’ve reopened the issue.
I am also getting the same error. Reverting back to
2.23.+
fixes the issue.Same issue here. Same goes for 2.25.0.
Reverting to 2.23.0 solves the problem.
Seems like you are loading the wrong version of ByteBuddy. What version are you using?
On Mon, 4 Feb 2019, 10:49 Taher, notifications@github.com wrote:
I am still getting this issue with Mockito
3.0.0
(andmockito-android
) when running Android instrumentation tests:That version is too low. Mockito depends on ByteBuddy 1.9.7: https://github.com/mockito/mockito/blob/aacacb156166b54b7eba3dfe03dd7bacda54c8d8/gradle/dependencies.gradle#L7 Also shown in the pom: http://central.maven.org/maven2/org/mockito/mockito-core/2.24.0/mockito-core-2.24.0.pom It seems like your project setup does not correctly resolve dependency mismatches.
Thank you very much.
resolutionStrategy { force(“com.linkedin.dexmaker:dexmaker:2.12.1”) }
My colleague run into the same issue after recent upgrade to ‘mockito-core’, version: ‘2.25.0’ ‘powermock-api-mockito2’, version: ‘2.0.0’ ‘powermock-module-junit4’, version: ‘2.0.0’.
Downgrading mockito-core to 2.23.0 helped to solve it. Strangely, it happens only running tests individually from IDEA, but they work fine running via Gradle script. Also, I was not able to reproduce it on my machine, though we have very similar environment setups.
Error message:
org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class com.fasterxml.jackson.databind.ObjectMapper.
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.191-b12 JVM name : Java HotSpot™ 64-Bit Server VM JVM version : 1.8.0_191-b12 JVM info : mixed mode OS name : Windows 10 OS version : 10.0
Underlying exception : java.lang.IllegalArgumentException: Could not create type
I would reopen this as an actual bug since 2.23.0 version doesn’t have this issue and the lib should handle gracefully its own dependencies like this one.
Had the same issue even though we never set bytebuddy as dependency somewhere. Setting bytebuddy dependency explicitly at gradle fixed it for me.
I think I may have found the culprit: the newer Dexmaker version MockK is using. I’ve managed to get rid of the issue in my project with the latest version of the two libs by doing this:
I have the same problem with 2.27.0, reverting back to 2.23.4 works.
@TimvdLippe @raphw I was able to solve this issue. I noticed that java 11 does not contain the JAXB APIs. So I included them as dependencies in pom.
Thanks for your help.
As explained in my issue #1671 we have the same problem (sorry for the duplicate, I hadn’t seen this one). After investigation, it seems that we have 2 ByteBuddy. The first one is from hibernate-core-5.4.1 and is released byte-buddy 1.9.5 The second one is from mockito-core-2.25.1 and is released byte-buddy 1.9.7
For some reasons, it’s the byte-buddy 1.9.5 from hibernate dependency that is used by mockito.
By exluding byte-buddy from hibernate dependency, we can fix the problem, but I’m not sure that we will not have any side effect on hibernate 😕
I had to add byte-buddy as well as an explicit dependency to our project. Is it valid behaviour to use byte-buddy during runtime, even though it’s specified as just a compile-time dependency of mockito?
Adding bytebuddy as a dependency did fix the problem. I would think Gradle manages this dependency himself.
Anyway, thanks for the help!
In Eclipse make sure to add as external library byte buddy. Ctrl + right click on project > Java Build Path > Libraries > Add external JARS > byte-buddy-1.**.*.jar This worked for me if you want to debug your Junit tests in Eclipse
This worked for me. I added
byte-buddy
andbyte-buddy-agent
dependencies to my pom file in the version 1.9.7.It was like the following:
Also, I excluded those dependencies in
hibernate-core
andhibernate-entitymanager
because those dependencies contains thebyte-buddy
andbyte-buddy-agent
in a prior version.We are getting this when attempting to upgrade Spring Boot 2.0.x to 2.1.x.
spring-boot-dependencies specifies the version for ByteBuddy and Mockito:
Spring Boot 2.0:
Spring Boot 2.1:
We are using
spy
on a real class and it is fully public and non-final.Any version of Mockito newer than 2.15.0 seems to cause this. 2.16.0 uses byte-buddy 1.9.10. Any idea on what I can look for?
If Hibernate wants 1.9.10 and mockito-core wants 1.9.7, and those versions are not compatible, then I’m not sure what you want the build system to do. It won’t be resolved automatically. Our options:
a) shadowing b) mockito-core bumps to 1.9.10 c) hibernate downgrades to 1.9.7 d) we tell customers to downgrade/force to 1.9.7 and hope that hibernate will work
Some of those options are clearly not viable.