172

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?

7
  • 2
    Check solution at stackoverflow.com/a/41964561/4903889
    – AskQ
    Dec 23, 2017 at 18:12
  • Upgrading to a newer version of Mockito helped me solve this problem. Mar 18, 2019 at 15:02
  • Clearing Gradle cache and downloading all dependencies from scratch fixed it for me. (See: stackoverflow.com/a/13567793/4568679 for clearing cache)
    – Slav
    Oct 15, 2019 at 11:26
  • I had a typo in the file src/test/resources/org/powermock/extensions/configuration.properties causing the exception shown. Jun 4, 2021 at 19:03
  • I invalidated cache of IDE and restarted, it solved the problem for me
    – setiabb
    May 20, 2022 at 9:54

39 Answers 39

115

If you are using powermock ensure that your dependencies point to:

org.powermock:powermock-api-mockito2

instead of

org.powermock:powermock-api-mockito
5
  • 3
    I don't know the exact cause yet, but this answer helped me when my tests failed after upgrading Spring version 1.5.14-RELEASE to 2.0.4-RELEASE
    – Kenny
    Sep 12, 2018 at 12:04
  • 3
    The question has nothing to do with powermock. The problem occurs when you use jre instead of jdk. Feb 8, 2019 at 16:14
  • 77
    I am not using PowerMock Jul 2, 2019 at 15:14
  • And if you aren't using PowerMock? The question did reference interface org.mockito.plugins.MockMaker. Dec 14, 2020 at 2:44
  • 4
    Why unrelated answers get so many upvotes?
    – Farid
    Jul 7, 2022 at 15:01
65

Missing reference to:

  • byte-buddy-1.6.5.jar
  • byte-buddy-agent-1.6.5.jar
  • objenesis-2.5.jar

Since Mockito 2.0.2 beta, Mockito-core has dependencies.

8
  • 25
    those libraries are included by default, this is not the solution
    – Eduardo
    Oct 13, 2017 at 14:11
  • I can confirm that adding the latest byte-budy (and more likely, the byte-buddy-agent) JARs to a "latest greatest" mockito setup help here. For me, the exception from the question showed up after enabling "mocking of final" classes with mockito 2.
    – GhostCat
    Apr 23, 2018 at 8:49
  • 4
    these are transitive dependencies you do not have to add them explicitly. Feb 6, 2019 at 12:45
  • 2
    thease deps are listed in the maven page too, at the bottom of the page. Including it resolve the problem.
    – Davide
    Dec 20, 2019 at 11:12
  • 3
    These are only included by default (as suggested by @Eduardo) if you are not using a build system (maven/gradle) to automatically resolve the dependencies, otherwise this could be the solution.
    – wlnirvana
    Dec 2, 2020 at 6:45
21

I had Byte Buddy on classpath (is transitive dep of Mockito 2.8.9) and still got the exception. Reason for me was that I ran the Unit tests with JRE instead of JDK. Switching to JDK worked for me.

6
  • 2
    How did you change this? Mar 8, 2019 at 5:40
  • 3
    @TaranmeetSingh if you're running with Android Studio, go to File > Project Structure > SDK location, and check the paths to your JDK are correct. (You'll need to download a JDK if you haven't already from the Oracle website.) May 15, 2019 at 7:54
  • Hi @LydiaRalph thanks for the suggestion actually I had a altogether different problem. I was writing Unit test cases for one of the modules(which are also standalone android studio projects) but I had instead opened the whole project in android studio. Switching to the stand alone project solved this for me. May 16, 2019 at 8:21
  • @Stefano , how to switch as I've not mentioned any where using jdk or jre
    – Tarun
    Aug 14, 2019 at 6:15
  • This fixed it for me. I just removed the JRE from Eclipse's Java Runtimes, leaving only the JDK. Dec 18, 2019 at 21:29
17

This problem with Mockito2 occurs if you enable the option to mock final classes.

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

or

  • add -javaagent:byte-buddy-agent-*.jar as a VM option
3
  • In Android Studio how do you check if you are using JRE instead of JDK, and how would you change it?
    – sbearben
    Apr 5, 2019 at 18:35
  • I have the same problem with Android Studio. Was anybody successful? Apr 10, 2020 at 21:31
  • Still seeing this issue what to do? Mar 10, 2021 at 13:54
17

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'
}
1
  • 1
    This worked for me, problems immediately resolved after upgrading from 2.7.2 to 4.2.0. Note that you still need to set up the mock-maker-inline extension.
    – BenLewis
    Jan 3, 2022 at 2:31
9

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.

1
  • There is another possibility: Hibernate also have net.bytebuddy* as a dependency, so may the dependencies being in conflict with each other.
    – ruzenhack
    Jun 15, 2018 at 21:48
6

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.

2
  • Do you have a solution without powermock? Dec 14, 2020 at 3:05
  • @portfoliobuilder if you're not using powermock, I don't think you will encounter the same issue I faced. Probably you could double check the other suggestions given on this thread? Make sure mockito is updated, that you're using jdk and you don't have the missing references.
    – Ely
    Dec 16, 2020 at 9:14
5

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
2
  • Same here. Best to avoid incompatibilities by using what the documentation says: github.com/powermock/powermock/wiki/Mockito-2-Maven
    – lauksas
    Dec 29, 2021 at 13:11
  • Removing mockito-core is a great solution, it also works for Java17. To make it fully working, need add following inside <configuration> of <plugin><groupId>org.apache.maven.plugins</groupId> </plugin> <argLine> --add-opens java.base/java.lang=ALL-UNNAMED ... ... </argLine>
    – XLi
    Nov 11, 2022 at 17:41
4

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.

3

Remove version declaration works for me:

Example:

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.8.9</version>
        <scope>test</scope>
    </dependency>

remove 2.8.9

After:

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <scope>test</scope>
    </dependency>

It may due to version issue between jar packages of this dependency.

3
  • 1
    you're surely inheriting the version because removing it will break the project. Wrong so wrong answer.
    – eduyayo
    Jun 28, 2019 at 12:00
  • This was oddly the solution for me. I think this must be because of a parent pom, in my case org.springframework.boot:spring-boot-starter-parent:2.4.0. Trying to use a higher version of Mockito led to this strange error, but letting the parent pom's dependency management seemed to alleviate the issue. I had tried to use Mockito 4.8.1 but the dependency management dropped it back to 3.6.0. Additionally, I read that there is some issue with IntelliJ and the 4.x version's use of byte-buddy.
    – jocull
    Nov 11, 2022 at 3:29
  • I had the same issue using gradle. This is how I solved: testImplementation 'org.mockito:mockito-core'
    – Digao
    Nov 18, 2022 at 20:05
3

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'
2

In my case, I was working on a project that does not use the maven build system. So this is what worked for me.

NB: (creating the library is optional, you can add the jars directly to your project build path)

Hope this helps someone.

2

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
...
1

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.

1
1

I encountered this issue and solved it by bumping my version of org.mockito.mockito-core to the latest release. I had been using an older release for quite some time.

1

Delete the "byte-buddy-1.7.11.jar" manually from the maven repository path which appears in the error. Maven update the project and the issue will be resolved. Tried and worked for me.

1

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>
1

For me the problem was that IntelliJ ran the test as integration test. So I needed to create JUnit test manually

enter image description here

1
  • Maybe you have this unit test in the androidTest directory? I presume AS expect to have instrumented tests in 'androidTest' and unit tests in 'test'.
    – Kikiwa
    Nov 20, 2020 at 16:45
1

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.

1

I had same error, removing the dependency version for mockito core and doing a fresh mvn clean install fixed the error

0

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

0

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>
0

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}
0

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.

0

Not the answer to the original poster, since s/he's using Netbeans, but in Eclipse I needed to do "Maven -> Update Project" in order to get tests working again.

0

It worked for me After I removed bytebuddy folder from ${home}.m2\repository\net\bytebuddy and removed byte-buddy-agent,byte-buddy and objenesis dependency from pom.xml

0

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'
 }
1
0
    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

0

In my case, removing:

{home}/.m2/repository/org/robolectric/

solved the issue. Next time it runs, it will download those files again.

0
  • 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("");

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.