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.

3.7.7 inline can not find MockMethodDispatcher - has worked in 3.2.4

See original GitHub issue

Hi @raphw ,

I am from Apache Cassandra project where we want to use Mockito to mock static methods, I know this feature was introduced in Mockito 3.4.0, we had version 3.2.4 so I had to update it.

My test where I was mocking statics works fine but update to that version broke other tests and I am not sure why. The exception I am getting all over again is this:

Dependency versions are:

mockito-inline:3.7.7 bytebuddy and bytebuddy-agent: 1.10.20 junit 4.12

I do not have any extension files etc, I just merely bumped versions, we had mockito-core but as I understand it mockito-inline already includes it so we do not depend on it explicitly.

Any suggestions how to resolve this would be appreciated.

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 com.sun.proxy.$Proxy26.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:60)
	at org.mockito.Mockito.spy(Mockito.java:2035)
	at org.apache.cassandra.distributed.mock.nodetool.InternalNodeProbe.connect(InternalNodeProbe.java:76)
	at org.apache.cassandra.distributed.mock.nodetool.InternalNodeProbe.<init>(InternalNodeProbe.java:60)
	at org.apache.cassandra.distributed.impl.Instance$DTestNodeTool.<init>(Instance.java:818)
	at org.apache.cassandra.distributed.impl.Instance.lambda$nodetoolResult$39(Instance.java:726)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@1a352001
	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:19)
	at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:16)
	at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:28)
	... 14 more
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:52)
	... 19 more
Caused by: java.lang.NoClassDefFoundError: org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:305)
	... 24 more
Caused by: java.lang.ClassNotFoundException: org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at org.apache.cassandra.distributed.shared.InstanceClassLoader.loadClassInternal(InstanceClassLoader.java:101)
	at org.apache.cassandra.distributed.shared.InstanceClassLoader.loadClass(InstanceClassLoader.java:87)
	... 25 more

It particularly fails on doing this spy

StorageServiceMBean mock = Mockito.spy(StorageService.instance);

What is interesting is that some code in MockMethodDispatcher is red like this, invalidating caches and restart of IDEA does not seem to help either.

What has happened between Mockito core 3.2.4 and inline 3.7.7 that it stopped to work? 3.4.0 does not work either …

https://ibb.co/rQ4Pkrg

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
TuomasKiviahocommented, Sep 24, 2021

Exceptions like these most commonly occur with corrupted .m2 repositories. Could you clear your .m2 repository for Mockito and ByteBuddy and reinstall the dependencies?

This has to do with MockMethodDispatcher.raw work-a-round (#845) albeit it indeed feels like the class file would be actually be missing from jar itself. The dynamically crafted mockitoboot.jar containing the actual class derived from the raw is indeed installed successfully to bootstrap classloader but in environments with sophisticated classloading mechanism like OSGi it may not be reachable by the InlineByteBuddyMockMaker or more specific the InlineBytecodeGenerator since the classloading test is not done with the classloader that is being actually used for loading InlineBytecodeGenerator.

https://github.com/robolectric/robolectric/issues/2677#issuecomment-266010717 explains the rationale behind the approach.

Caused by: java.lang.NoClassDefFoundError: org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher
        at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<init>(InlineDelegateByteBuddyMockMaker.java:317)
        at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:25)
0reactions
raphwcommented, Apr 17, 2022

I think that it does not correctly resolve the resource. The class loader is queried for getResourceAsStream(name). If this call returns null, the error in question is raised.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mockito - Bountysource
3.7.7 inline can not find MockMethodDispatcher - has worked in 3.2.4 $ 0. Created 1 year ago in mockito/mockito with 0 comments. Hi...
Read more >
Mockito 3.2.4 API - javadoc.io
It allows access to documentation straight from the IDE even if you work offline. ... Be aware that you cannot use the inline...
Read more >
Migrating from Mockito 2.28 to 3.7 and many tests immediately ...
If that works, then the culprit is the inline mockmaker. If that doesn't work, could you try out different versions until you bisect...
Read more >
Mockito cannot mock this class - java - Stack Overflow
Are you doing anything interesting with your classes or tests, like loading dynamically from a JAR? (Or any other reason that you'd have...
Read more >
Mockito Inline - Maven Repository
Mockito preconfigured inline mock maker (intermediate and to be superseeded by automatic usage in a ... Ranking, #345 in MvnRepository (See Top Artifacts)...
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