我使用Testng& Mockito& Powermock(不使用Junit 3或4)测试我的类JeebiesKwCheckUtil
,其中包含静态和最终版本,我已经完成了代码和pom作为Powermock介绍,但它仍然无法正常工作,总是显示org.powermock.api.mockito.ClassNotPreparedException
,它让我疯了......
代码:
我把它写成Powermock& mockito Intro。 Link就在这里;
@PrepareForTest(JeebiesKwCheckUtil.class)
public class JeebiesKwCheckUtilTest {
@Test
public void testMethodA() {
PowerMockito.mockStatic(JeebiesKwCheckUtil.class); // Exception here
// MethodA use stticMethodB
Mockito.when(JeebiesKwCheckUtil.staticMethodB).thenReturn(Result);
Assert.assertEquals(JeebiesKwCheckUtil.MethodA......)
}
}
Pom文件: 这里有4个Maven Jar,Powermock的版本是1.7.1
<properties>
<powermock.version>1.7.1</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.8.9</version>
<scope>test</scope>
</dependency>
</dependencies>
错误:
org.powermock.api.mockito.ClassNotPreparedException:
[Ljava.lang.Object;@29df4d43
The class com.yidian.serving.util.JeebiesKwCheckUtil not prepared for test.
at org.powermock.api.mockito.expectation.reporter.MockitoPowerMockReporter.classNotPrepared(MockitoPowerMockReporter.java:32)
at org.powermock.api.mockito.internal.mockcreation.MockTypeValidatorFactory$DefaultMockTypeValidator.validate(MockTypeValidatorFactory.java:38)
at org.powermock.api.mockito.internal.mockcreation.AbstractMockCreator.validateType(AbstractMockCreator.java:10)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:56)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46)
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:73)
at com.yidian.serving.util.JeebiesKwCheckUtilTest.testCheckSensitiveWord(JeebiesKwCheckUtilTest.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:571)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.runSuites(TestNG.java:1028)
at org.testng.TestNG.run(TestNG.java:996)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
任何人都可以给我一个解决方案或adivse吗?非常感谢〜
答案 0 :(得分:2)
我已经得到了朋友的帮助,
将PowerMockTestCase
扩展为注入&#39;亚军&#39;就像Junit(PowerMockRunner ......)
@PrepareForTest(JeebiesKwCheckUtil.class)
public class JeebiesKwCheckUtilTest extends PowerMockTestCase{...}
聚苯乙烯。如果你遇到了一个&#34; SSL&#34;像我那样的问题,添加@ ..在课前忽略
@PowerMockIgnore("javax.net.ssl.*")
这个问题与TestNG + Mockito + PowerMock - verifyStatic() does not work完全相同,但我也遇到了SSL问题,这个问题搞砸了......