PowerMock and Mockito casue UnfinishedVerificationException



我一起使用PowerMock和Mockito。我有以下内容包括:

import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.*;

这是我的测试类的佼佼者:

@RunWith(PowerMockRunner.class) 

然后我有一个测试,包括以下检查:

verifyNew(ExternalNode.class, atLeastOnce());
verify(nodeResolutor).comm();

但是我收到一个未完成的验证异常异常。但是,如果我颠倒两个指令,一切正常。怎么可能?

下面是异常堆栈跟踪:

org.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock) here:
-> at org.powermock.api.mockito.internal.invocation.MockitoNewInvocationControl.verify(MockitoNewInvocationControl.java:95)
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.
    at com.janny.MyTest.verifyResults(MyTest.java:89)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

可能是什么问题?我能做什么?

verifyNew返回 PowerMock 希望您调用的 ConstructorArgumentsVerification 对象。

verifyNew(ExternalNode.class, atLeastOnce()).withNoArguments();
    /* or */
verifyNew(ExternalNode.class, atLeastOnce()).withArguments("foo", "bar");

反序之所以有效,是因为 PowerMock 只能在下次与 PowerMock、Mockito 或模拟交互时检测到语法是否错误。当您的verifyNew排在第一位时,PowerMock 会在后续verify中被调用并找出问题所在。此代码还将产生相同的异常:

verify(nodeResolutor).comm();
verifyNew(ExternalNode.class, atLeastOnce()); /*BAD: no withNoArguments here*/
verify(nodeResolutor).anyOtherMethod();

像这样捕获未完成的验证和存根是@After方法(或@RunWith(MockitoJUnitRunner.class,在 PowerMock 之外执行相同操作(中调用Mockito.validateMockitoUsage()有意义的部分原因,但我不知道它是否会在这里捕获 PowerMock 问题。

相关内容

  • 没有找到相关文章

最新更新