Java PowerMock和Mockito未完成验证异常

Java PowerMock和Mockito未完成验证异常,java,mockito,testng,powermock,powermockito,Java,Mockito,Testng,Powermock,Powermockito,我很难让PowerMock和Mockito工作 我正在使用这些版本。应该是 powermock core v 1.7.4 powermock api mockito v 1.7.4 powermock-module-junit4 v1.7.4 mockito所有版本1.10.19 我也在使用TestNG,但我不认为这对测试结果有任何影响 我想模拟一个对象,在模拟对象上调用一个方法,并验证在模拟对象方法中是否调用过一次私有方法 我构建了一个例子,希望能解释我想要实现的目标 public cla

我很难让PowerMock和Mockito工作

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

  • powermock core v 1.7.4
  • powermock api mockito v 1.7.4
  • powermock-module-junit4 v1.7.4
  • mockito所有版本1.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是错误的。

我自己无法在junit4上复制这个问题。原来这是TestNG的问题。

当您使用TestNG运行测试时,@RunWith注释被忽略,因为它是JUnit特有的

要实现此功能,您有两个选项:

  • 使用JUnit运行测试

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


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

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(<Class_containing_pvt_method>.class)
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(.class)
    
    @RunWith注释将powermock初始化为junit的运行程序


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

    我看到的第一件事是,您可能应该准备
    MyClass
    ,而不是
    TestClass
    。看看能不能帮上忙?很好。不幸的是,当我写这个问题时,这只是一个打字错误。我已经修复了打字错误。我无法复制此问题。当我运行它时,它工作正常。我只是需要稍微更改一下匹配器。在您报告无法复制此问题后,我发现了此问题。问题是毕竟TestNG。当我使用junit4时,一切都很完美。谢谢大家的建议,通常都是些小事;)报告您无法重现问题通常是解决方案。您可以用您的评论创建一个答案,我将接受它。