添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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?

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. – Jeff Baranski Jun 4, 2021 at 19:03

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

org.powermock:powermock-api-mockito2

instead of

org.powermock:powermock-api-mockito
                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
                The question has nothing to do with powermock. The problem occurs when you use jre instead of jdk.
– Łukasz Rzeszotarski
                Feb 8, 2019 at 16:14
                And if you aren't using PowerMock? The question did reference interface org.mockito.plugins.MockMaker.
– portfoliobuilder
                Dec 14, 2020 at 2:44
                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
                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
                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
                @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.)
– Lydia Ralph
                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.
– Taranmeet Singh
                May 16, 2019 at 8:21
                This fixed it for me. I just removed the JRE from Eclipse's Java Runtimes, leaving only the JDK.
– Guillaume F.
                Dec 18, 2019 at 21:29
  

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

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

    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.

    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

    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.

    @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

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

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

    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.

    I had the same issue and was also able to resolve by explicitly adding the latest ByteBuddy to the POM. Maven isn't resolving the transitive dependency correctly. See here: github.com/mockito/mockito/issues/2629 – Martin Wunderlich Jul 10, 2023 at 6:47

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

    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.