

Utilize it in situations when you need to capture varargs or when the verified method was called many times. This is the code, which is as follows: interface MyInterface Example of capturing Varargs untPeople(new Person("John"), new Person("Jane") //vararg methodĪssertEquals(expected, peopleCaptor.getAllValues()) Make an interface and a doSth function to it that can take a string list as an argument.
#Mockito captor how to#
The following illustration shows how to properly capture collection arguments.


List expected = asList(new Person("John"), new Person("Jane")) ĪssertEquals(expected, peopleCaptor.getAllValues()) Using ArgumentCaptor with a List/Collection Verify(mock).countPeople(peopleCaptor.capture()) Verify(mock).doSomething(argument.capture()) ĪssertEquals("John", argument.getValue().getName()) Įxample of capturing varargs: untPeople(new Person("John"), new Person("Jane") //vararg methodĪrgumentCaptor peopleCaptor = ArgumentCaptor.forClass(Person.class) ArgumentCaptor argument = ArgumentCaptor.forClass(Person.class) The following code uses an ArgumentCaptor to verify the argument passed into the doSomething method. The getAllValues() function returns ListT> and getValue() returns T. If an ArgumentCaptor object catches arguments for many invocations, call getAllValues().
