Introduction
This post will look at the ArgumentCaptor of Mockito and how to use it in our unit tests.
We will use the following two classes for the examples in this post. We have a Dog class with a method to name the dog and an Owner class with a method to adopt a dog and name it. We will use ArgumentCaptor to capture the name the owner will give to the dog.
|
|
Using ArgumentCaptor
An ArgumentCaptor captures the argument passed to a method. For our example, we will use it to capture a string argument. This way, we can verify if the argument passed to the method is what we expected it to be.
To create an ArgumentCaptor, we can use this:
|
|
This code will create an ArgumentCaptor to capture strings. Next, we need to capture the arguments, which can be done within a Mockito.verify
.
|
|
The entire unit test to verify the name of the dog looks like this:
|
|
ArgumentCaptor annotation
We can also use annotations to create an ArgumentCaptor for us. We need to add an annotation above our test class to do that.
- For Junit5, you need to add
@ExtendWith(MockitoExtension.class)
. - For Junit4, you need to add
@RunWith(MockitoJUnitRunner.class)
.
I will be using Junit5 for the example. To create an ArgumentCaptor with an annotation, we only need to add this annotated field to the class:
|
|
Mockito will initialize this with an ArgumentCaptor that captures String.
With annotations, our unit test class will look like this:
|
|
Java 8 alternative for ArgumentMatcher
For less complex use cases, we can also use Lambda matcher to verify the input of a method. With argThat
,
we create a matcher that validates the input of a method. In the example below, we create this matcher argThat(name -> "Max".equals(name))
that verifies that the name of the dog is Max.
|
|
This is better for less complex use cases because it makes your unit tests easier to read and write. See this post if you want to know more about mockito lambda matchers
Conclusion
In this post, we looked at how to use the argumentCaptor in a Unit test. We also looked at the argumentMatcher as an alternative to verify the input to our unit tests.
Further reading
More about testing in Java: