Methods of the ArgumentCaptor Class
Some of the useful methods and their descriptions of the argument captor class are as follows:
-
T capture()
It is used to capture the method arguments. This method must be used inside of verification.
Returns:
null or default values
-
ArgumentCaptor<U> forClass(Class<S>clazz)
It builds a new argument captor.
Type Parameters:
S - Type of clazz
U - Type of object captured by the newly built ArgumentCaptor
Parameters:
class - Type matching the parameter to be captured.
Returns:
A new ArgumentCaptor
-
List<T> getAllValues()
It returns all the captured values if multiple arguments were captured. When the varargs method was called multiple times, this method returns a merged list of all values from all invocations.
Returns:
captured argument value
-
T getValue()
It returns all the captured values of the argument. If the verified method was called multiple times, then this method returns the latest captured value.
Returns:
captured argument value
Mockito ArgumentCaptor Example
Let’s say we define a class as shown in the below code:
class MathUtils {
public int add(int x, int y) {
return x + y;
}
public boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
}
return true;
}
public long squareLong(long l) {
return l * l;
}
}
Then using ArgumentCaptor, the tests for the above code can be written as follows:
@Test
void test() {
MathUtils mockMathUtils = mock(MathUtils.class);
when(mockMathUtils.add(1, 1)).thenReturn(2);
when(mockMathUtils.isInteger(anyString())).thenReturn(true);
ArgumentCaptor acInteger = ArgumentCaptor.forClass(Integer.class);
ArgumentCaptor acString = ArgumentCaptor.forClass(String.class);
assertEquals(2, mockMathUtils.add(1, 1));
assertTrue(mockMathUtils.isInteger("1"));
assertTrue(mockMathUtils.isInteger("999"));
verify(mockMathUtils).add(acInteger.capture(), acInteger.capture());
List allValues = acInteger.getAllValues();
assertEquals(List.of(1, 1), allValues);
verify(mockMathUtils, times(2)).isInteger(acString.capture());
List allStringValues = acString.getAllValues();
assertEquals(List.of("1", "999"), allStringValues);
}
FAQs
What does ArgumentCaptor do in mockito?
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.
How does mockito mock work?
With Mockito, you create a mock, tell Mockito what to do when specific methods are called on it, and then use the mock instance in your test instead of the real thing.
Why do we need Mockito?
Mockito framework is used to simplify the development of a test by mocking external dependencies and use them in the test code.
Which annotation is used to create an ArgumentCaptor?
The @Captor annotation is used to create an ArgumentCaptor instance which is used to capture method argument values for further assertions.
Can mockito mock asynchronous classes?
Mockito can mock asynchronous classes.
Must Recommended Topic - Difference between argument and parameter
Conclusion
In this article, we have extensively discussed Mockito ArgumentCaptor.
We hope that this blog has helped you enhance your knowledge regarding Mockito ArgumentCaptor and if you would like to learn more, check out our articles on
Mockito Annotations, Creating Mocks and need of mocking, Testing exceptions.
Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc.
Enrol in our courses and refer to the mock test and problems available.
Take a look at the interview experiences and interview bundle for placement preparations.
Do upvote our blog to help other ninjas grow.
Happy Coding!