1

I' trying to mock a static method call using powermockito in testng with following dependencies. It works fine when we have setup methods annotated with @BeforeClass and getting the following exception when @BeforeTest is used.

[INFO] +- org.testng:testng:jar:7.0.0:test
[INFO] |  \- com.beust:jcommander:jar:1.72:test
[INFO] +- org.powermock:powermock-module-testng:jar:2.0.2:test
[INFO] |  +- org.powermock:powermock-core:jar:2.0.2:test
[INFO] |  |  +- org.powermock:powermock-reflect:jar:2.0.2:test
[INFO] |  |  +- org.javassist:javassist:jar:3.24.0-GA:test
[INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.9.3:test
[INFO] |  |  \- net.bytebuddy:byte-buddy-agent:jar:1.9.3:test
[INFO] |  \- org.powermock:powermock-module-testng-common:jar:2.0.2:test
[INFO] \- org.powermock:powermock-api-mockito2:jar:2.0.2:test
[INFO]    +- org.powermock:powermock-api-support:jar:2.0.2:test
[INFO]    \- org.mockito:mockito-core:jar:2.23.0:test
[INFO]       \- org.objenesis:objenesis:jar:2.6:test

@PrepareForTest(TestStatic.class)
@SuppressStaticInitializationFor("com.test.TestStatic")
public class StaticTest extends PowerMockTestCase {

     @BeforeTest  //---> This Works if replaced with @BeforeClass instead of @BeforeTest.Dont understand why
     public void setup(){

          PowerMockito.mockStatic(TestStatic.class); //---> Exception at this line
    }

    @Test
    public void testAB() throws Exception {

        String a = "test";
        PowerMockito
                .when(TestStatic.getData(anyString()))
                .thenReturn(a);
        TestStatic.getData("123");
    }
}

But I got the following exception

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)
    at com.sun.proxy.$Proxy20.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:238)
    at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:68)
    at org.mockito.Mockito.mock(Mockito.java:1895)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodInvocationControl(DefaultMockCreator.java:108)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.doCreateMock(DefaultMockCreator.java:61)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:53)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:40)
    at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:62)
    at com.test.StaticTest.setup(StaticTest.java:32)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@a4ca3f6
    at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:54)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:57)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:44)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:21)
    at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:18)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
    ... 44 more
Caused by: java.lang.ClassCastException: Cannot cast org.powermock.api.mockito.mockmaker.PowerMockMaker to org.mockito.plugins.MockMaker
    at java.lang.Class.cast(Class.java:3369)
    at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:50)
    ... 49 more
second
  • 4,069
  • 2
  • 9
  • 24
Shiny
  • 161
  • 1
  • 1
  • 9
  • Possible duplicate of [Could not initialize plugin: interface org.mockito.plugins.MockMaker](https://stackoverflow.com/questions/41956692/could-not-initialize-plugin-interface-org-mockito-plugins-mockmaker) – racraman Sep 06 '19 at 13:22
  • 1
    You might want to add your `PowerMockTestCase` and `TestStatic` to your question. What `Runner` do you use to execute this test? – second Sep 06 '19 at 19:37

0 Answers0