PowerMock 和 Mockito UnfinishedVerificationException

PowerMock and Mockito UnfinishedVerificationException

我很难让 PowerMock 和 Mockito 工作。

我正在使用这些版本。他们应该是 compatible

我也在使用 TestNG,但我认为这不会对本次测试的结果产生任何影响。

我想模拟一个对象,在模拟对象上调用一个方法,并验证在模拟对象方法中调用了一次私有方法。

我构建了一个示例,希望能解释我想要完成的任务。

public class MyClass {

    public void execute(Object o) {
        valid(o);
    }

    private boolean valid(Object o) {
        return o != null;
    }
}

这是我的测试class

@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 错误。

我自己无法在 junit4 上重现该问题。原来是TestNG的问题。

当您使用 TestNG 来 运行 您的测试时,@运行With 注释被忽略,因为它特定于 JUnit。

要完成这项工作,您有 2 个选择:

  1. 运行 你的 JUnit 测试。

  2. 或配置 TestNG,使其使用 PowerMock 对象工厂。配置细节解释得很好 .

确保在使用 powermock 测试私有方法时添加了这两个注释:

@RunWith(PowerMockRunner.class)
@PrepareForTest(<Class_containing_pvt_method>.class)

@RunWith 注释将 powermock 初始化为 junit 的运行程序

@PrepareForTest 注解在使用 final、private、static 或 native 方法模拟 final 类、类 时使用,以便 powermock 可以从字节读取- 这些 类 的代码(因为 java 不允许对私有方法进行反射)。