Use Mockito to verify that nothing is called after the method.

I use Mockito to write unit test in Java, and I would like to verify that the specific method is the last one to call the object.

I am doing something similar in the test code:

row.setSomething(value); row.setSomethingElse(anotherValue); row.editABunchMoreStuff(); row.saveToDatabase(); 

In my layout, I don’t care about the order in which I edit everything in the line, but it is very important that I do not try to do anything else after I save it. Is there a good way to do this?

Please note that I am not looking for confirmation of NoMoreInteractions: it does not confirm that saveToDatabase is the last thing called and it also fails if I call something on a line that I am not explicitly checking. I would like to say something like:

 verify(row).setSomething(value); verify(row).setSomethingElse(anotherValue); verifyTheLastThingCalledOn(row).saveToDatabase(); 

If this helps, I go to Mockito from the JMock test that did this:

 row.expects(once()).method("saveToDatabase").id("save"); row.expects(never()).method(ANYTHING).after("save"); 
+41
java unit-testing mockito mocking
Feb 04 '09 at 17:00
source share
3 answers

I think this requires more user work.

 verify(row, new LastCall()).saveToDatabase(); 

and then

 public class LastCall implements VerificationMode { public void verify(VerificationData data) { List<Invocation> invocations = data.getAllInvocations(); InvocationMatcher matcher = data.getWanted(); Invocation invocation = invocations.get(invocations.size() - 1); if (!matcher.matches(invocation)) throw new MockitoException("..."); } } 

Previous answer:

You're right. verifyNoMoreInteractions is what you need.

 verify(row).setSomething(value); verify(row).setSomethingElse(anotherValue); verify(row).editABunchMoreStuff(); verify(row).saveToDatabase(); verifyNoMoreInteractions(row); 
+51
Feb 05 '09 at 3:43
source share

Not 100% off topic, but I was just looking to find the opposite of validation, and that was the only relevant result, it ends up being after Mockito.verifyZeroInteractions (mock);

Just do away with someone else looking here ...

+9
Jan 27 2018-11-11T00:
source share

This question led me to some enhancements to the validation API in JMockit (available in the next release 0.983).

The solution I came across allows you to write (in a test method):

 new VerificationsInOrder() {{ unverifiedInvocations(); row.saveToDababase(); }}; 

... if you only want to verify that some method is called after everything else. To verify that this happens before all other calls, simply move the call to the top. This really applies to any sequence of consecutive calls.

If, in addition to the above check, you also want to check that some other methods are called in any order, a second block of checks can be added to the test (before or after another block this does not matter)

 new Verifications() {{ row.setSomething(value); row.setSomethingElse(anotherValue); }}; 

Although it's a little long due to the use of anonymous inner classes, this syntax is simple and flexible; notice how it adds structure to the test and avoids repeating method calls (e.g. verify(...) ). This is more than described here (Hamcrest matchers, invocation counts, etc.), and this is not limited to checking instance methods (static methods and constructors can be mocked and checked the same way).

0
Jun 29 '09 at 0:01
source share



All Articles