缺少对验证(模拟)的方法调用,但是有一个吗?

Missing method call for verify(mock), but there is one?

简介

我正在尝试进行测试以验证(使用 Mockito v1.9.5 的 verify)接口 Bar 中具有签名 deinit() 的方法在之后被调用执行传递 Foo.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 的调用,这是一个 final 方法并且 Mockio 不支持 final 方法 mocking 作为解释 into the doc. If you want to verify it you need either to remove the keyword final from the declaration of BarImpl#deinit or use Powermock 而不是。

就我而言,我得到这个是因为我试图在间谍(而不是模拟)上执行 verify() 。我在做:

mySpy = Mockito.spy(<<constructor>>);

我不得不这样做:

mySpy = mock(MyClass.class, delegatesTo(<<constructor>>)