java PowerMock和Mockito未完成验证异常
我很难让PowerMock和Mockito工作
我正在使用这些版本。它们应该是compatible
- powermock core v 1.7.4
- powermock api mockito v1.7.4
- powermock-module-junit4 v1.7.4
- mockito all v1.10.19
我也在使用TestNG,但我不认为这对测试结果有任何影响
我想模拟一个对象,在模拟对象上调用一个方法,并验证在模拟对象方法中是否调用过一次私有方法
我构建了一个例子,希望能解释我想要实现的目标
public class MyClass {
public void execute(Object o) {
valid(o);
}
private boolean valid(Object o) {
return o != null;
}
}
这是我的测试课
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)
public class TestClass {
@After
public void validate() {
validateMockitoUsage();
}
@Test
public void executeTest() throws Exception {
//Initialize the Class and create the spy
MyClass myClass = PowerMockito.spy(new MyClass());
//Execute the method which I wish to test
myClass.execute(null);
//I want to verify that the private valid method was called once.
PowerMockito.verifyPrivate(myClass, times(1)).invoke("valid", anyObject());
}
}
我得到以下错误:
org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at TestClass.executeTest(MyClass.java:14)
Example of correct verification:
verify(mock).doSomething()
Also, this error might show up because you verify either of:
final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
at TestClass.validate(MyClass.java:60)
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.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
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:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
我试过搜索,但找不到解决问题的方法。我认为这是一个常见错误,我使用PowerMock/Mockito是错误的
# 1 楼答案
我无法在junit4上复制这个问题。事实证明,这是TestNG的一个问题
# 2 楼答案
当您使用TestNG运行测试时,@RunWith注释被忽略,因为它是JUnit特有的
要实现这一点,您有两个选项:
用JUnit运行测试
或者配置TestNG,使其使用PowerMock对象工厂。配置细节解释得很好here
# 3 楼答案
确保在使用powermock测试私有方法时添加了这两个注释:
@RunWith注释将powermock初始化为junit的运行程序
@PrepareForTest注释用于模拟final类,即带有final、private、static或native方法的类,以便powermock可以读取这些类的字节码(因为java不允许对私有方法进行反射)