Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 986
Danubian Sailor Avatar asked Jul 02 '14 09:07

Danubian Sailor


People also ask

How do you verify a method in Mockito?

Verify in Mockito simply means that you want to check if a certain method of a mock object has been called by specific number of times. When doing verification that a method was called exactly once, then we use: verify(mockObject).someMethodOfMockObject(someArgument);

How to verify multiple arguments in Mockito based unit tests?

We can use assertEquals (expected, result) to verify that expected multiple arguments match with the retrieved values from ArgumentCaptor. Drop me your questions related to invoking a method multiple times and verify it’s method different arguments values in mockito based unit tests.

How to test number of method invocations for a mocked method?

We can use Mockito verify methods at the end of the testing method code to make sure that specified methods are called. Mockito verify () method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method.

How do I check if Mock is called or not?

Mockito – Verifying Method Calls. We can use org.mockito.Mockito.verify(T mock) method to ensure whether a mock () method was called with required arguments or not. In other words, we can say that Mockito.verify(T mock) is used to confirm that specific interactions took place.


1 Answers

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.

like image 104
brass monkey Avatar answered Oct 23 '22 04:10

brass monkey