Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm getting following exception once tests is started:
Testcase: treeCtorArgumentTest(com.xythos.client.drive.cachedtree.CachedTreeTest): Caused an ERROR
Could not initialize plugin: interface org.mockito.plugins.MockMaker
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66)
at com.sun.proxy.$Proxy7.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:168)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:162)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:64)
at org.mockito.Mockito.mock(Mockito.java:1687)
at org.mockito.Mockito.mock(Mockito.java:1600)
at com.xythos.client.drive.cachedtree.CachedTreeTest.setUp(CachedTreeTest.java:51)
Caused by: java.lang.NoClassDefFoundError: net/bytebuddy/dynamic/loading/ClassLoadingStrategy
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.<init>(SubclassByteBuddyMockMaker.java:33)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.<init>(ByteBuddyMockMaker.java:22)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:54)
at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:18)
at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:17)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.dynamic.loading.ClassLoadingStrategy
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
This is my test:
package com.xythos.client.drive.cachedtree;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class CachedTreeTest {
public CachedTreeTest() {
@Test
public void treeCtorArgumentTest() {
somemock m = mock(SomeClass.class);
I'm using NetBeans 8.2. I've downloaded mockito-core-2.7.0.jar, and then I select "Test Libraries"->"Add Jar" and added mockito-core-2.7.0.jar.
Classpath and everything looks ok, I',m still getting exception.
Any advice?
–
–
If you are using powermock ensure that your dependencies point to:
org.powermock:powermock-api-mockito2
instead of
org.powermock:powermock-api-mockito
–
–
–
–
–
–
–
–
–
This means when in your test/resources/mockito-extensions directory
you have the file called org.mockito.plugins.MockMaker with the
following content mock-maker-inline.
In that case byte-buddy, which is a transitive dependency for mockito-core, has the problem to attach own agent to the java process. But the problem occurs only when you use JRE.
The solution would be either:
Use JDK instead of JRE
add -javaagent:byte-buddy-agent-*.jar
as a VM option
–
Well, after a lot of research , solution for me is super simple :
update Mockito lib version to latest
dependencies {
testImplementation 'org.mockito:mockito-core:3.10.0'
androidTestImplementation 'org.mockito:mockito-android:3.10.0'
–
I had the same problem - same stacktrace appear in my log. It is typically problem with project setup... OR
The problem can be in the bytebuddys JARs if these were not downloaded correctly.
When I try to check class ClassLoadingStrategy manually then I get zip error.
In that case it is just enough manually delete the Byte Buddy from local maven directory, usually located at:
{home}/.m2/repository/net/bytebuddy/
The next try to run project or test they will be downloaded again and should work as expected.
Unfortunatelly common Java ClassLoader faces in the same way when class or jar missing as well as the jar is corrupted.
–
Problem: PowerMock + Mockito + TestNG + PowerMockTestCase
Sharing my problem/solution in case it helps anybody.
My dependencies were all pointed correctly:
testImplementation 'org.mockito:mockito-core:2.8.47'
testImplementation 'org.powermock:powermock-core:1.7.4'
testImplementation 'org.powermock:powermock-module-testng:1.7.4'
testImplementation 'org.powermock:powermock-api-mockito2:1.7.4'
But I still got the following error:
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66)
at com.sun.proxy.$Proxy11.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:186)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:180)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
at org.mockito.Mockito.mock(Mockito.java:1729)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
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:57)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:69)
My test was something as such:
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import static org.mockito.MockitoAnnotations.initMocks;
@PrepareForTest(MyClass.class)
public class MyTest extends PowerMockTestCase {
@BeforeTest
public void init() {
initMocks(this);
As mentioned in this thread, removing the initMocks()
method removes the error, but all the mocks become null.
✅ Solution: BeforeTest VS BeforeMethod
What I found out for my case is that @BeforeTest
was actually posing a problem.
Changing it to @BeforeMethod
resolved the error.
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import static org.mockito.MockitoAnnotations.initMocks;
@PrepareForTest(MyClass.class)
public class MyTest extends PowerMockTestCase {
@BeforeMethod // CHANGE THIS!
public void init() {
initMocks(this);
My guess is that it is something to do with beans injection;
@BeforeTest
is executed before any beans got injected while @BeforeMethod
is executed after beans injection. Not sure how it really affected though.
–
In my case, after removing mockito-core
, the test worked!
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
My version is here, and JDK is 1.8.0_121.
<properties>
<powermock.version>2.0.7</powermock.version>
<mockito.version>2.8.9</mockito.version>
</properties>
Now my dependencies are:
powermock-api-mockito2
powermock-module-junit4
–
–
For android development you need to import a couple of things:
dependencies {
testCompile "org.mockito:mockito-core:+"
androidTestCompile "org.mockito:mockito-android:+"
could not find any reference of this but this is the only one that has worked for me.
I had this problem when I was using mockitokotlin2. I solved it by using latest version of libraries:
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
testImplementation 'org.mockito:mockito-inline:4.4.0'
–
–
–
In my case, I was working on a project that does not use the maven build system. So this is what worked for me.
Navigated to the maven repo for mockito (used v2.26): https://mvnrepository.com/artifact/org.mockito/mockito-core/2.26.0. I downloaded the jar.
On the same page at the bottom, I looked up the dependencies. For mockito 2.26.0, these dependencies are:
Byte Buddy v.1.9.10 (https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy/1.9.10)
Byte Buddy Java Agent v1.9.10 (https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy-agent/1.9.10)
Objenesis v2.6 (https://mvnrepository.com/artifact/org.objenesis/objenesis/2.6)
I downloaded the jar files for the above mockito dependencies
In Eclipse I created a user library containing the four jar file and added it to my project.
NB: (creating the library is optional, you can add the jars directly to your project build path)
Hope this helps someone.
I solved this problem by removing jmockit in POM XML for Maven. After that my Junit 5 tests work perfectly.
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.41</version>
<scope>test</scope>
</dependency>
With this dependency I always get an error like:
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)
Caused by: com.sun.tools.attach.AttachNotSupportedException: no providers installed
I had the same issue and tryed @Paskas's solution, worked well Junit Test ok at first.
added the bytebuddy dependency to my pom.xml:
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-dep</artifactId>
<version>1.9.7</version>
</dependency>
Hope this helps.
–
Mockito 2 requires dependencies. Adding the two ByteBuddy dependencies below. solved the issue for me:
byte-buddy-x.x.xx.jar
byte-buddy-agent-x.x.xx.jar
In my case I include jar files in the project, which can be found at:
https://bintray.com/raphw/maven/ByteBuddy/
If you need it for a Maven project, simply add:
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.9.14</version>
<type>pom</type>
</dependency>
–
You're probably having issues with manually adding dependencies to your project. Take note of the mockito-core jar version being used in your project.
Visit this mvn repository link
https://mvnrepository.com/artifact/org.mockito/mockito-core/{replace-with-your-mockito-core-version}
Move to the compile dependencies sections down below in the mvn repository page, download all the appropriate version of the dependencies mockito-core needs.
As of answering this question:
byte-buddy
byte-buddy-agent
objenesis
Add all to the project together with mockito-core and run tests.
in my case, some abnormal env setting will also lead to this error:
in ubuntu 16.04, java and javac had been update-alternatives to 7
but $JAVA_HOME had been wrongly set to 8
i remove the $JAVA_HOME, and everything back to normal
I had the same exception when switch to Java 10 and Spring Boot 2.
This dependency combination worked for me:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.8.16</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.8.16</version>
<scope>test</scope>
</dependency>
Adding a bnd aspect to this
Adding on to Paskas' answer, you also have to include the dependencies in the cnf maven repository, if you're using one (like my cnf/central.mvn
).
org.mockito:mockito-core:2.21.0
net.bytebuddy:byte-buddy:1.8.15
net.bytebuddy:byte-buddy-agent:1.8.15
org.objenesis:objenesis:2.6
and for convenient referencing, you can include a bnd variable in your cnf/build.bnd
mockito: org.mockito:mockito-core; version=2.21.0,\
net.bytebuddy:byte-buddy; version=1.8.15,\
net.bytebuddy:byte-buddy-agent; version=1.8.15,\
org.objenesis:objenesis; version=2.6
and in your project's bnd file
-testpath:\
${mockito}
I have had the same problem in one existing project.
I used java 8 and additionally installed java 10.
So I started get this exception.
I deinstalled java 10, it has not helped.
I deleted bytebuddy jars from maven repo, it has not helped.
Only after complete removal of all jars from maven repo I could compile project again.
I had the same issue, got resolved by adding jcenter in repositories and mockito dependecies.
repositories {
jcenter()
dependencies {
testImplementation 'org.mockito:mockito-core:3.7.7'
testImplementation 'org.powermock:powermock-core:1.7.4'
testImplementation 'org.powermock:powermock-module-testng:1.7.4'
testImplementation 'org.powermock:powermock-api-mockito2:1.7.4'
androidTestImplementation 'org.mockito:mockito-core:2.19.0'
testImplementation "org.mockito:mockito-core:2.19.0"//game changer for your question
This second line was a game-changer, android Test implementation and test implementation are not the same test set (android test implementation is a test set for physical devices or emulators)...
you may have dependency for one and writing code, but if you run the different test (local in the android studio without emulator) it will fail (unless..:)
happy testing
PowerMock doesn't work with sonar in the first step, the cause of the problem is the combination of powermock and jacoco
We will not face this issue locally, but if we use sonar for test coverage calculations, the error is as follows
Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
In PowerMock 2.0.9 version, a workaround for this problem is provided they have added an annotation @PrepareOnlyThisForTest
, this provides the ability for the isolation of a particular Test class.
Apart from that, we need to add delegate the Runner to SpringRunner using @PowerMockRunnerDelegate(SpringRunner.class)
Add Power Mock Dependencies in pom.xml
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
Use the PowerMock Junit Runner for test Class and declare the test class that we’re mocking and delegate SpringRunner
@RunWith(PowerMockRunner.class)
@PrepareOnlyThisForTest(ServiceApplication.class)
@PowerMockRunnerDelegate(SpringRunner.class)
public class ExampleControllerTest {
PowerMockito.mockStatic(ServiceApplication.class);
Mockito.when(ServiceApplication.getStatic()).thenReturn("");
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.