If you are working on Spring boot applications that automatically include Mockito Core dependency and are facing this error, then you may try the suggested solution.
- The root cause of this error is the version mismatch between Mockito and ByteBuddy.
- For a few people, it turned out to be JDK configuration. Make sure the IDE is NOT pointing to a JRE. It should be a JDK path.
1. Problem
Mockito core depends on a library called byte-buddy, and this problem mostly occurs when mockito doesn’t find a matching byte-buddy jar version. The error looks like this:
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:74) ~[mockito-core-2.23.4.jar:na]
at com.sun.proxy.$Proxy61.getHandler(Unknown Source) ~[na:na]
at org.mockito.internal.util. MockUtil.isMock(MockUtil.java:81) ~[mockito-core-2.23.4.jar:na]
...
...
Caused by: java.lang.NoClassDefFoundError: net/bytebuddy/dynamic/loading/ ClassInjector$UsingReflection
at org.mockito.internal.creation.bytebuddy. SubclassInjectionLoader.<init>(SubclassInjectionLoader.java:28) ~[mockito-core-2.23.4.jar:na]
at org.mockito.internal.creation.bytebuddy. SubclassByteBuddyMockMaker.<init>(SubclassByteBuddyMockMaker.java:33) ~[mockito-core-2.23.4.jar:na]
at org.mockito.internal.creation.bytebuddy. ByteBuddyMockMaker.<init>(ByteBuddyMockMaker.java:21) ~[mockito-core-2.23.4.jar:na]
...
...
[spring-test-5.1.4.RELEASE.jar:5.1.4.RELEASE]
... 19 common frames omitted
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.dynamic. loading.ClassInjector$UsingReflection
at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_171]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_171]
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_171]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_171]
... 42 common frames omitted
2. Solution
Find out the mockito core version your project is using. In Eclipse, you can check in the project build path by navigating to:
Right click on project -> Properties -> Java build path -> Libraries tab

Search the maven repository for that version of mockito core. To me, it was: https://mvnrepository.com/artifact/org.mockito/mockito-core/2.23.4
Look at the Compile Dependencies section. Note down the correct dependent version of byte-buddy and include in the project. If the jar is already included with some other version, override the version with this correct version.

Build the application and run the tests again. Your issue related to MockMaker must be solved by now.
Happy Learning !!
Thanks! This solved my problem
Thanks a lot!!!! Adding byte-buddy dependency solved the problem:)
It worked for me.
Thanks! Including of byte-buddy dependency helped me.
I had installed many version of JDK and JRE, I switched to correct JRE in Properties/Java Build Path/Libraries and works.
in my case I had to change the JRE libray to JDK
check Properties/Java Build Path/Libraries
In my case it was an even weirder problem. Somehow, don’t really know why, the directory where IntelliJ was looking for the JDK got changed somewhere along the way. Result was that, overnight, all tests stopped working while throwing this exact same error.
After struggling with it for a day I finally came up with the solution which was going to File->Project Structure->SDKs, there I chose the JDK the project was set to (Java 11 in my case), and found out it was pointing to a whole different place, I just redirected it to the folder where my JDK was and everything went back to normal.
Hope it helps!
Thanks for sharing.