Search code examples
javamockitojunit4

mockito does not verify that my method is invoked


I have the following two statements in my test class

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

the second one works correctly and returns the passed value, which I use later in my tests. But the first one raises an error like the following:

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

What could be wrong?


Solution

  • The general usage pattern in mockito is:

    when(mock.doSomething()).then ...
    
    doTheThingYouAreTesting();
    
    verify(mock).doSomething();
    

    You appear to have your verify first. Try changing your code to follow the pattern above.