我正在学习 GwtMockito,但在我的一项测试中无法获得一致的 verify() 方法。
我正在尝试测试我的应用程序正在触发正确的 GwtEvents。所以我在@Before 方法中像这样模拟了事件总线:
eventBus = mock(HandlerManager.class);
此测试按预期通过:
// Passes as expected
verify(eventBus).fireEvent(any(ErrorOccurredEvent.class));
我想强制测试失败只是为了知道它运行正确。所以我把它改成了这个,它仍然通过:
// Expected this to fail, but it passes
verify(eventBus).fireEvent(any(ErrorOccurredEvent.class));
verifyZeroInteractions(eventBus).fireEvent(any(ErrorOccurredEvent.class));
这对我来说似乎很矛盾。所以我删除了第一个测试:
// Fails as expected
verifyZeroInteractions(eventBus).fireEvent(any(ErrorOccurredEvent.class));
最后我添加了一个不相关的事件,应该导致它失败
// Expected to fail, but passes
verify(eventBus).fireEvent(any(ErrorOccurredEvent.class));
verify(eventBus).fireEvent(any(ModelCreatedEvent.class)); // This event is not used at all by the class that I'm testing. It's not possible for it to be fired.
我没有找到任何解释正在发生的事情的文档。ErrorOccurredEvent 和 ModelCreatedEvent 都扩展了 GwtEvent,并且已经在手动测试中得到验证。我是否错误地测试了我的 EventBus?如果是这样,有什么更好的方法来解决它?
更新
我做了一些额外的实验。这似乎是我在使用 Mockito 匹配器时遇到的问题。当我让测试失败时,异常会报告方法签名,eventBus.fireEvent(<any>)
因此它似乎没有考虑到我传递给 any 方法的不同类。尚不确定该怎么做,但将其包括在此处以供其他研究此问题的人使用。