ArgumentCaptor allows us to capture an argument passed to a method in order to inspect it. This is especially useful when we can't access the argument outside of the method we'd like to test.
We can use @Captor annotation to create argument captor at field level.
The @Captor annotation is used to create an ArgumentCaptor instance which is used to capture method argument values for further assertions. Note that mockito verifies argument values using the equals() method of argument class.
I agree with what @fge said, more over. Lets look at example. Consider you have a method:
class A {
public void foo(OtherClass other) {
SomeData data = new SomeData("Some inner data");
other.doSomething(data);
}
}
Now if you want to check the inner data you can use the captor:
// Create a mock of the OtherClass
OtherClass other = mock(OtherClass.class);
// Run the foo method with the mock
new A().foo(other);
// Capture the argument of the doSomething function
ArgumentCaptor<SomeData> captor = ArgumentCaptor.forClass(SomeData.class);
verify(other, times(1)).doSomething(captor.capture());
// Assert the argument
SomeData actual = captor.getValue();
assertEquals("Some inner data", actual.innerData);
The two main differences are:
ArgumentCaptor
can capture more than once.To illustrate the latter, say you have:
final ArgumentCaptor<Foo> captor = ArgumentCaptor.forClass(Foo.class);
verify(x, times(4)).someMethod(captor.capture()); // for instance
Then the captor will be able to give you access to all 4 arguments, which you can then perform assertions on separately.
This or any number of arguments in fact, since a VerificationMode
is not limited to a fixed number of invocations; in any event, the captor will give you access to all of them, if you wish.
This also has the benefit that such tests are (imho) much easier to write than having to implement your own ArgumentMatcher
s -- particularly if you combine mockito with assertj.
Oh, and please consider using TestNG instead of JUnit.
The steps in order to make a full check are :
First, prepare the argument captor :
ArgumentCaptor<ArgumentClass> argumentCaptor = ArgumentCaptor.forClass(ArgumentClass.class);
Second, verify the call to the dependent on component (collaborator of subject under test).
times(1) is the default value, so ne need to add it.
verify(dependentOnComponent, times(1)).method(argumentCaptor.capture());
Third, get the argument passed to collaborator using getValue() of the captor
ArgumentClass someArgument = messageCaptor.getValue();
Fourth, use someArgument for assertions
I created this example that simulates a very simple service that uses a repository to save a String (no dependency injection, no entities), just to teach ArgumentCaptor quickly.
3 classes: PersonService, PersonRepository and PersonServiceTest (packages omitted)
public class PersonService {
private PersonRepository personRepository;
public void setPersonRepository(final PersonRepository personRepository) {
this.personRepository = personRepository;
}
public void savePerson(final String name) {
this.personRepository.save(name.toUpperCase().trim());
}
}
public class PersonRepository {
public void save(final String person) {
System.out.println(".. saving person ..");
}
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
class PersonServiceTest {
@Test
void testPersonService() {
// Create the repository mock
final PersonRepository personRepositoryMock = mock(PersonRepository.class);
// Create the service and set the repository mock
final PersonService personService = new PersonService();
personService.setPersonRepository(personRepositoryMock);
// Save a person
personService.savePerson("Mario ");
// Prepare an ArgumentCaptor to capture the value passed to repo.saveMethod
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
// Capture the argument passed in the unique method invocation
verify(personRepositoryMock, times(1)).save(captor.capture());
// Check if the captured value is the expected one
final String capturedParameter = captor.getValue();
assertEquals("MARIO", capturedParameter);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With