gpt4 book ai didi

java - TestNG + Mockito + PowerMock 始终显示 "ClassNotPreparedException"

转载 作者:行者123 更新时间:2023-11-30 02:12:31 25 4
gpt4 key购买 nike

我使用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)

任何人都可以给我一个解决方案或建议吗?非常非常感谢~

最佳答案

在 friend 的帮助下我完成了,

扩展 PowerMockTestCase 以注入(inject)“Runner”,就像 Junit(PowerMockRunner...)

@PrepareForTest(JeebiesKwCheckUtil.class)
public class JeebiesKwCheckUtilTest extends PowerMockTestCase{...}

诗。如果你和我一样遇到了“SSL”问题,请在课前添加@..Ignore

@PowerMockIgnore("javax.net.ssl.*")

这个问题与 TestNG + Mockito + PowerMock - verifyStatic() does not work 完全相同,但我也遇到了 SSL 问题,这让一切变得困惑......

关于java - TestNG + Mockito + PowerMock 始终显示 "ClassNotPreparedException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49624076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com