jeudi 9 avril 2015

How to verify the order of mocks without explicitly defining the methods?

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);
}


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?


Aucun commentaire:

Enregistrer un commentaire