Could not initialize plugin: interface org.mockito.plugins.MockMaker

后端 未结 25 2229
走了就别回头了
走了就别回头了 2020-12-29 01:01

I\'m getting following exception once tests is started:

    Testcase: treeCtorArgumentTest(com.xythos.client.drive.cachedtree.CachedTreeTest):  Caused an ERR         


        
25条回答
  •  死守一世寂寞
    2020-12-29 01:08

    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.

提交回复
热议问题