mockito 不验证我的方法被调用

mockito does not verify that my method is invoked

我的测试中有以下两条语句class

    Mockito.verify(customvalueProcessorFactory, times(1)).get(customvalueKey, userId);
    when(customvalueProcessorFactory.get(customvalueKey, userId)).thenReturn(customvalueProcessor);

第二个工作正常,returns 传递的值,我稍后在测试中使用它。但是第一个引发如下错误:

-> at <my-path>.MessageProcessorUnitTest.expectCustomvalueProcessorFactoryGetCalledWillReturn(MessageProcessorUnitTest.java:194)
Actually, there were zero interactions with this mock.

有什么问题吗?

mockito 中的一般使用模式是:

when(mock.doSomething()).then ...

doTheThingYouAreTesting();

verify(mock).doSomething();

您似乎先进行了验证。尝试更改您的代码以遵循上述模式。