我正在使用TestNG进行单元测试并模拟我的应用程序。
我的问题是,当我在PowerMockito.whenNew(ClassB.class)
中使用ClassA
方法并进行调试时,我发现objB
是原始未模拟的方法。
经过多次调查,我用了
import org.junit.Test;
而不是
import org.testng.annotations.Test;
并尝试使用Junit测试而不是TestNG。 令人惊讶的是,它使用JUnit(666)返回了预期值,但是返回了TestNG(15),这是预期的。
所有类和pom文件都在
之下package unit.test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.testng.annotations.Test;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ ClassA.class, ClassB.class })
public class App {
@Test
public void shoudlReturn666() throws Exception {
ClassB mockB = PowerMockito.mock(ClassB.class);
PowerMockito.when(mockB.get15()).thenReturn(666);
PowerMockito.whenNew(ClassB.class).withNoArguments().thenReturn(mockB);
Assert.assertTrue(new ClassA().callB() == 666);
}
}
A类 -
package unit.test;
public class ClassA {
public int callB() {
ClassB objB = new ClassB();
return objB.get15();
}
}
B类 -
package unit.test;
public class ClassB {
public int get15() {
return 15;
}
}
Pom文件
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-mockito-release-full</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>
</dependencies>
结果是:
[TestNG] Running:
C:\Users\Wael Elkholy\AppData\Local\Temp\testng-eclipse--514551319\testng-customsuite.xml
FAILED: shoudlReturnTheCValue
junit.framework.AssertionFailedError: null
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertTrue(Assert.java:27)
at unit.test.App.shoudlReturnTheCValue(App.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:696)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:882)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1189)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
at org.testng.SuiteRunner.run(SuiteRunner.java:254)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@2752f6e2: 59 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@48140564: 20 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@c818063: 65 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@7dc36524: 127 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 18 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@2133c8f8: 18 ms
我的代码有问题吗?
答案 0 :(得分:1)
根据the PowerMock documentation:
@PrepareForTest({ ClassA.class, ClassB.class })
public class App extends PowerMockTestCase {
@Test
public void shoudlReturn666() throws Exception {
ClassB mockB = PowerMockito.mock(ClassB.class);
PowerMockito.when(mockB.get15()).thenReturn(666);
PowerMockito.whenNew(ClassB.class).withNoArguments().thenReturn(mockB);
Assert.assertTrue(new ClassA().callB() == 666);
}
}