无法在@BeforeTest

时间:2019-09-06 12:57:24

标签: java mockito testng powermock powermockito

我试图在testng中使用powermockito模拟具有以下依赖项的静态方法调用。当我们有用@BeforeClass注释的设置方法并使用@BeforeTest时出现以下异常时,它可以很好地工作。

[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");
    }
}

但是我遇到了以下异常

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

0 个答案:

没有答案