question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mockito 3.11.2 (?) fails with "can't load this type of class file"

See original GitHub issue

Broken version: 3.11.2? Working versions: 3.10.0

Description

All was working OK with 3.8.0 and we upgraded to 3.11.2 but this presented the problem below. I have sequentially downgraded and it seems 3.10.0 doesn’t present me with any problems.

I have not been able to confidently isolate it to this version because I have also experienced #2316 and #2327 in the interim versions.

Our context is Android and a com.android.test instrumented test module. We are using org.mockito:mockito-core, plus org.mockito:mockito-android with the same version, and org.mockito.kotlin:mockito-kotlin:3.2.0.

2021-06-22 21:35:57.029 9913-9913/ourapp.package.debug E/MonitoringInstr: Exception encountered by: ourapp.package.testing.integrations.TestApplication@b4bbfaf. Dumping thread state to outputs and pining for the fjords.
    org.mockito.exceptions.base.MockitoException: 
    Failed to release mocks
    
    This should not happen unless you are using a third-part mock maker
        at ourapp.package.testing.integrations.TestApplication.onCreate(OurAppTestRunner.kt:44)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192)
        at androidx.test.runner.MonitoringInstrumentation.callApplicationOnCreate(MonitoringInstrumentation.java:428)
        at android.app.ActivityThread.handleMakeApplication(ActivityThread.java:7507)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7446)
        at android.app.ActivityThread.access$1500(ActivityThread.java:301)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2148)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
        at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:84)
        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
        at $Proxy3.isTypeMockable(Unknown Source)
        at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:33)
        at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
        at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:250)
        at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:232)
        at org.mockito.internal.MockitoCore.mock(MockitoCore.java:83)
        at org.mockito.Mockito.mock(Mockito.java:1954)
        at org.mockito.internal.configuration.MockAnnotationProcessor.processAnnotationForMock(MockAnnotationProcessor.java:66)
        at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:27)
        at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:24)
        at org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:45)
        at org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:73)
        at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:74)
        at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:48)
        at org.mockito.MockitoAnnotations.openMocks(MockitoAnnotations.java:82)
        at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:100)
        at ourapp.package.testing.integrations.TestApplication.onCreate(OurAppTestRunner.kt:44) 
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192) 
        at androidx.test.runner.MonitoringInstrumentation.callApplicationOnCreate(MonitoringInstrumentation.java:428) 
        at android.app.ActivityThread.handleMakeApplication(ActivityThread.java:7507) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7446) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:301) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2148) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8512) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
     Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@67d84a2
        at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:57)
        at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:65)
        at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:50)
        at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:26)
        at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:20)
        at org.mockito.internal.configuration.plugins.Plugins.getAnnotationEngine(Plugins.java:67)
        at org.mockito.internal.configuration.GlobalConfiguration.tryGetPluginAnnotationEngine(GlobalConfiguration.java:57)
        at org.mockito.MockitoAnnotations.openMocks(MockitoAnnotations.java:81)
        at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:100) 
        at ourapp.package.testing.integrations.TestApplication.onCreate(OurAppTestRunner.kt:44) 
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192) 
        at androidx.test.runner.MonitoringInstrumentation.callApplicationOnCreate(MonitoringInstrumentation.java:428) 
        at android.app.ActivityThread.handleMakeApplication(ActivityThread.java:7507) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7446) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:301) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2148) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8512) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
     Caused by: java.lang.reflect.InvocationTargetException
2021-06-22 21:35:57.029 9913-9913/ourapp.package.debug E/MonitoringInstr:     at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:52)
        	... 21 more
     Caused by: java.lang.ExceptionInInitializerError
        at net.bytebuddy.utility.dispatcher.JavaDispatcher.of(JavaDispatcher.java:104)
        at net.bytebuddy.description.type.TypeDescription$ForLoadedType.<clinit>(TypeDescription.java:8282)
        at net.bytebuddy.description.type.TypeDescription$ForLoadedType.of(TypeDescription.java:8355)
        at net.bytebuddy.implementation.bind.annotation.Super$Binder.<clinit>(Super.java:222)
        at net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$ParameterBinder.<clinit>(TargetMethodAnnotationDrivenBinder.java:166)
        at net.bytebuddy.implementation.MethodDelegation.withDefaultConfiguration(MethodDelegation.java:601)
        at net.bytebuddy.implementation.MethodDelegation.to(MethodDelegation.java:271)
        at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.<init>(SubclassBytecodeGenerator.java:64)
        at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.<init>(SubclassBytecodeGenerator.java:74)
        at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.<init>(SubclassByteBuddyMockMaker.java:37)
        at org.mockito.android.internal.creation.AndroidByteBuddyMockMaker.<init>(AndroidByteBuddyMockMaker.java:22)
        	... 24 more
     Caused by: java.lang.IllegalStateException: Failed to create invoker for net.bytebuddy.utility.dispatcher.JavaDispatcher$Invoker
        at net.bytebuddy.utility.dispatcher.JavaDispatcher$DynamicClassLoader.invoker(JavaDispatcher.java:1270)
        at net.bytebuddy.utility.dispatcher.JavaDispatcher$Invoker$CreationAction.run(JavaDispatcher.java:431)
        at net.bytebuddy.utility.dispatcher.JavaDispatcher$Invoker$CreationAction.run(JavaDispatcher.java:420)
        at java.security.AccessController.doPrivileged(AccessController.java:43)
        at net.bytebuddy.utility.dispatcher.JavaDispatcher.<clinit>(JavaDispatcher.java:65)
        	... 35 more
     Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
        at java.lang.ClassLoader.defineClass(ClassLoader.java:591)
        at net.bytebuddy.utility.dispatcher.JavaDispatcher$DynamicClassLoader.invoker(JavaDispatcher.java:1266)

I appreciate the above may not be enormously helpful, and I’m happy to try and provide more information on request.

Mocked subjects by this runner are:

  • an open class
  • a sealed class
  • various public interfaces

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
raphwcommented, Jun 22, 2021

Arghs, indeed. Fixed on master of Byte Buddy. Needed a fallback for the reflection dispatcher if dynamic class generation is not supported. Byte Buddy started to create an intermediate dispatcher in a separate class loader to avoid reflective calls to stem from its own context, otherwise method handle lookups might leak, for instance. For Android, this does of course break things.

Will be fixed in the next version.

1reaction
TimvdLippecommented, Jun 22, 2021

Some more outfall in the Android ecosystem @raphw 😭

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito cannot mock this class - java - Stack Overflow
Using Java 11, I solved this issue by including the following dependency in build.gradle : ... For me the issue was that the...
Read more >
Mockito 3.11.2 API - javadoc.io
This implementation is due to static type safety imposed by the java compiler. The consequence is that you cannot use anyObject() , eq()...
Read more >
java.lang.illegalargumentexception: unsupported class file major ...
I'm using a Java 18 early access build and while using mockito-core:3.11.2 all my tests were running fine, but when I switched to...
Read more >
Mockito cannot mock this class: class android.content.Context
MyTest > FirstTest FAILED org.mockito.exceptions.base. ... Byte Buddy could not instrument all classes within the mock's type hierarchy. This problem should ...
Read more >
How to Fix A "Mockito cannot mock this class" Exception in A ...
How to fix a MockBean error on a CrudRepository interface in a Spring Boot app using Java 11. Tagged with java, springboot, mockito....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found