Problem with Mockito - exceptions.verification.WantedButNotInvoked

joaosavio picture joaosavio · Jun 22, 2010 · Viewed 8.8k times · Source

When I put a "VerificationModeFactory.times(2)" in test before, when I run all tests of the class appears this exception:

org.mockito.exceptions.verification.WantedButNotInvoked: Wanted but not invoked: serviceService.getServices();

If I run each test separately or remove "VerificationModeFactory.times(2)" all works.

It's very weird. Could anybody help me?

Answer

Lunivore picture Lunivore · Jun 23, 2010

It looks as if your verification is declared statically - once per test class, instead of once per test method.

Do this:

verify(mock, times(2)).did("my thing");

or in your case

verify(mockService, times(2)).getServices();

(BTW, is it important that your class gets the service twice, or is it more important that it used it? Unless you're describing a performance fix, I'd probably concentrate on the value the service provides instead. Using Mockito that way helps keep tests flexible. My 2 cents.)