Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Mockito to verify that nothing is called after a method

I'm using Mockito to write a unit test in Java, and I'd like to verify that a certain method is the last one called on an object.

I'm doing something like this in the code under test:

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

In my mock, I don't care about the order in which I edit everything on the row, but it's very important that I not try to do anything more to it after I've saved it. Is there a good way to do this?

Note that I'm not looking for verifyNoMoreInteractions: it doesn't confirm that saveToDatabase is the last thing called, and it also fails if I call anything on the row that I don't explicitly verify. I'd like to be able to say something like:

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

If it helps, I'm switching to Mockito from a JMock test that did this:

row.expects(once()).method("saveToDatabase").id("save"); row.expects(never()).method(ANYTHING).after("save"); 
like image 591
Moss Collum Avatar asked Feb 04 '09 17:02

Moss Collum


People also ask

What does verify method do 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: ? 1. verify(mockObject).

Which method in Mockito verifies that no interaction has happened with a mock in Java?

Mockito verifyZeroInteractions() method It verifies that no interaction has occurred on the given mocks. It also detects the invocations that have occurred before the test method, for example, in setup(), @Before method or the constructor.

Does Mockito when call the method?

A mock does not call the real method, it is just proxy for actual implementations and used to track interactions with it. A spy is a partial mock, that calls the real methods unless the method is explicitly stubbed. Since Mockito does not mock final methods, so stubbing a final method for spying will not help.


1 Answers

I think it requires more custom 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 are right. verifyNoMoreInteractions is what you need.

verify(row).setSomething(value); verify(row).setSomethingElse(anotherValue); verify(row).editABunchMoreStuff(); verify(row).saveToDatabase(); verifyNoMoreInteractions(row); 
like image 184
Kent Lai Avatar answered Oct 21 '22 10:10

Kent Lai