简体   繁体   中英

How to verify all method calls in Mockito?

I've got a JUnit test with a few methods using Mockito testing. I have quite a plenty experience with EasyMock (although not recent) so I've organized it like that:

@Before
public void before() {
    myAPI = mock(MyAPI.class);
}

@After
public void after() {
    verify(myAPI);
    reset(myAPI);
}

The methods register some calls on mock, if they need them.

But I've got an exception:

org.mockito.exceptions.misusing.UnfinishedVerificationException: Missing method call for verify(mock) here: -> at de.datev.shared.spdvz.util.UserInfoUtilTest.after(UserInfoUtilTest.java:45)

Example of correct verification: verify(mock).doSomething()

I know I could do verify(myAPI).doSomething(someArgs) but:

  1. I'd have to do it in each method
  2. I'd have to repeat the same arguments, as those used in when

Is there any method to simply verify, if all the methods that were registered on when were actually called? EasyMock works that way, and it was very convinient...

If I have aa lot of calls that are exactly the same I just use a commonMocking-method, put the verify there and reuse it where needed. Then I keep the verfies in the actual test - this also makes it a lot more readable what the actual differences between each testcase is.

I think this is not possible. Here is an old discussion where two of the maintainers are stating their opinion on such a feature. The discussion is pretty old but as far as i can tell there still isn't support for that in the current Mockito version.

In Mockito verification is explicit. So you basically verify(assert) if a particular invocations has already taken place or not and it's up to you to choose what's important to be examined. Mockito has also much stronger separation between stubbing and mocking functionality. Thus, it let you stub some calls to make the code pass the path you intended without verifying the calls have happened. Moreover, you are not obligated to sub invocations unless you are happy with default values Mockito provides you.

Such an approach separates test setup logic from test verification one much better increasing readability.

and

Few words about why mockito doesn't promote verifying stubbed methods.

  1. Simplification. Back in the old days, when mock frameworks were widely unknown we've been hand-writing our stubs. But I don't remember a single case where we wanted to verify that the stub was actually called. The reason was simple: stubbed methods are implicitly verified: by the fact that stubbed value is essential in processing.

  2. I saw tests where not having separation of stubbing and 'expecting' was actually harmful. The developer added return-value expectation because mock screamed with UnexpectedOperation. But he did not check if the value returned by mock was actually used... Simplifying stubbing puts accent on something more interesting: if stubbed value is used correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM