Reputation: 7215
Here is the method to be tested:
protected void myMethod(final MyObject object) {
object.setX(...);
object.setY(...);
myObjectRepository.update(object);
}
In order to verify the order of calls -so that the repository is called after all setter calls- i needed to make a mock of MyObject (since inOrder works just with mocks). At the end it should look like this:
@Mock
private MyObjectRepository myObjectRepositoryMock;
@Test
public void testMyMethod() {
MyObject myObjectMock = mock(MyObject.class);
InOrder inOrder = Mockito.inOrder(myObjectMock, myObjectRepositoryMock);
// Run Test .....
inOrder.verify(myObjectMock);
inOrder.verify(myObjectRepositoryMock).update(myObjectMock);
}
.. but we see this exception:
.. UnfinishedVerificationException
Missing method call for verify(mock) here:
...
Example of correct verification:
verify(mock).doSomething()
Since i do not have to verify the order of setter calls, i would just group them together and say sth like "first this mock, than that method of that mock with that argument should be called"..
I do not want to define the exact order like this:
inOrder.verify(myObjectMock).setX(..);
inOrder.verify(myObjectMock).setY(..);
inOrder.verify(myObjectRepositoryMock).update(myObjectMock);
Is there a way of doing this?
Upvotes: 0
Views: 368
Reputation: 328594
You can verify the fields with assertEquals
inside of doAnswer()
object.setX(-1);
Mockito.doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) throws Throwable {
assertEquals(1, object.getX());
return null;
}
}).when(myObjectRepositoryMock).update(object);
That way, the order of calls doesn't matter; all that matters is that at the time of the update()
call, the correct value is in the correct place.
Upvotes: 2