我想验证我的方法是否以固定顺序使用不同的参数调用。我试过这个:
org.mockito.Mockito.verify(mock).myMethod(arg1);
org.mockito.Mockito.verify(mock).myMethod(arg2);
//was myMethod called with arg1 before it was called with arg2?
但这并没有考虑到顺序。是否有捷径可寻?
您可以使用ArgumentCaptor。这是 Mockito Javadoc 的一个片段:
ArgumentCaptor<Person> peopleCaptor = ArgumentCaptor.forClass(Person.class);
verify(mock, times(2)).doSomething(peopleCaptor.capture());
List<Person> capturedPeople = peopleCaptor.getAllValues();
assertEquals("John", capturedPeople.get(0).getName());
assertEquals("Jane", capturedPeople.get(1).getName());