介绍
我正在尝试进行一个测试,以验证(使用Mockito v1.9.5的验证)在执行传递Foo.deinit()之后调用接口Bar中具有签名deinit()的方法,并且我我碰到一个我真的不明白的错误.
这是我尝试运行的FooTest.java:
@RunWith(JukitoRunner.class)
public class FooTest {
@Inject
private Foo foo;
@Inject
private Bar bar;
public static class TestModule extends JukitoModule {
@Override
protected void configureTest() {
bind(Foo.class).to(FooImpl.class);
bind(Bar.class).to(BarImpl.class);
bindSpy(BarImpl.class);
}
}
@Test
public void testDeinit() {
foo.init(mock(Baz.class));
foo.deinit();
verify(bar).deinit();
}
@After
public void validate() {
validateMockitoUsage(); //line YY
}
}
运行此命令时,testDeinit()失败,并显示以下错误:
org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at redacted.impl.BarImpl.deinit(BarImpl.java:XX)
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 redacted.impl.FooTest.validate(FooTest.java:YY)
at org.jukito.InjectedStatement.evaluate(InjectedStatement.java:96)
at org.jukito.InjectedAfterStatements.evaluate(InjectedAfterStatements.java:58)
at org.jukito.jukitoRunner.run(JukitoRunner.java:197)
我一直无法从中提取很多有用的信息.似乎错误似乎抱怨到verify(bar).deinit()可能最后也没有.deinit(),我可以删除该部分并得到相同的错误.错误消息中提供的示例尤其令人沮丧,因为它看起来几乎与我使用的verify相同.
细节
这是我的BarImpl.java
public class BarImpl implements Bar {
private final Qux qux;
private final Quux quux;
@Inject
public BarImpl(final Qux qux, final Quux quux) {
this.qux = qux;
this.quux = quux;
}
@Override
private final void init(Baz baz) {
quux.init(this);
qux.init();
}
@Override
public final void deinit() {
qux.deinit(); //line XX
}
}
我仍然不清楚qux.deinit()如何在这里导致失败.这是我的FooImpl.java:
class FooImpl implements Foo {
private final Bar bar;
@Inject
public FooImpl(final Bar bar) {
this.bar = bar;
}
@Override
public void init(Baz baz) {
bar.init(baz);
}
@Override
public void deinit() {
bar.deinit();
}
}
题
是什么引起了UnfinishedVerificationException,如何解决?
我是Mockito新手,所以很可能我错过了一些基本知识.请让我知道我是否可以提供更多信息.抱歉,如果您已经回答了这个问题,那么我在这里误解了答案.
解决方法:
实际上,您的问题出在错误消息中:
Also, this error might show up because you verify either of:
final/private/equals()/hashCode() methods. Those methods cannot be
stubbed/verified.
实际上,您尝试隐式验证对BarImpl#deinit的调用,这是一个最终方法,而Mockio不支持into the doc所述的最终方法模拟.如果要对其进行验证,则需要从BarImpl#deinit的声明中删除关键字final.或改用Powermock.