开发者

Problem with Mockito - exceptions.verification.WantedButNotInvoked

开发者 https://www.devze.com 2023-01-04 03:48 出处:网络
When I put a \"VerificationModeFactory.times(2)\" in test before, when I run all tests of the class appears this exception:

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 te开发者_运维百科st separately or remove "VerificationModeFactory.times(2)" all works.

It's very weird. Could anybody help me?


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消