本文介绍了缺少验证(模拟)方法调用,但有一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行一个测试来验证(使用 Mockito v1.9.5 的 verify)接口 deinit() 的方法>Bar 在执行传递 Foo.deinit() 后被调用,我遇到了一个我真的不明白的错误.

I'm attempting to make a test which verifies (using Mockito v1.9.5's verify) that a method with signature deinit() in an interface Bar is called after execution of a pass-through Foo.deinit(), and I'm hitting an error I really don't understand.

这是我试图运行的 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() 失败并出现以下错误:

When running this, testDeinit() fails with the following error:

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 几乎相同.

Which I haven't been able to extract much useful information from. It seems almost as if the error complains that verify(bar).deinit() might as well not have a .deinit() off the end, and I can remove that portion and get an identical error. The example provided in the error message is especially frustrating, as it appears almost identical to my use of 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:

I'm still unclear how the qux.deinit() causes failure here. Here's my 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 新手,所以很可能我错过了一些基本的东西.如果我能提供更多信息,请告诉我.抱歉,如果已经回答了这个问题并且我误解了 SO 上的答案.

I'm a Mockito newb so it's very likely I've missed something fundamental. Please let me know if there's more information I can provide. Sorry if this has been answered already and I've misunderstood answers here on SO.

推荐答案

其实你的问题出在错误信息上:

Actually your problem is in the error message:

此外,由于您验证了以下任一项,因此可能会出现此错误:final/private/equals()/hashCode() 方法.那些方法不能存根/验证.

实际上,您尝试隐式验证对 BarImpl#deinit 的调用,这是一个 final 方法,而 Mockio 没有不支持 final 方法模拟,如进入文档.如果您想验证它,您需要从 BarImpl#deinit 的声明中删除关键字 final 或使用 Powermock 代替.

Indeed you try to implicitly verify calls to BarImpl#deinit which is a final method and Mockio doesn't support final method mocking as explained 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 instead.

这篇关于缺少验证(模拟)方法调用,但有一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 13:04