【问题标题】:PowerMock and Mockito UnfinishedVerificationExceptionPowerMock 和 Mockito UnfinishedVerificationException
【发布时间】:2019-05-31 05:08:42
【问题描述】:

我无法让 PowerMock 和 Mockito 正常工作。

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

  • powermock-core v 1.7.4
  • powermock-api-mockito v 1.7.4
  • powermock-module-junit4 v 1.7.4
  • mockito-all v 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 错误。

【问题讨论】:

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

标签: java mockito testng powermock powermockito


【解决方案1】:

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

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

  1. 使用 JUnit 运行测试。

  2. 或配置 TestNG 以便它使用 PowerMock 对象工厂。配置细节解释得很好here

【讨论】:

    【解决方案2】:

    我自己无法在 junit4 上复制该问题。事实证明这是 TestNG 的问题。

    【讨论】:

      【解决方案3】:

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

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

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

      @PrepareForTest 注解用于模拟最终类、具有最终、私有、静态或本机方法的类,以便 powermock 可以从这些类的字节码中读取(因为 java 不允许反射私有方法)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-20
        • 1970-01-01
        • 2011-08-27
        • 1970-01-01
        相关资源
        最近更新 更多