Recall & Review
beginner
What is an argument captor in unit testing?
An argument captor is a tool used in unit testing to capture the arguments passed to a mock method, so you can inspect or assert their values later.
Click to reveal answer
beginner
How do you declare an argument captor in JUnit with Mockito?
You declare it using @Captor annotation or by calling ArgumentCaptor.forClass(ClassName.class). For example: @Captor ArgumentCaptor<String> captor;
Click to reveal answer
intermediate
Why use argument captors instead of verifying method calls directly?
Argument captors let you check the exact values passed to a method, especially when you want to assert on complex objects or multiple calls, which simple verification cannot do.Click to reveal answer
intermediate
Show a simple example of capturing an argument with Mockito in JUnit.
Example:
1. Declare captor: @Captor ArgumentCaptor<String> captor;
2. Call method on mock.
3. Verify and capture: verify(mock).method(captor.capture());
4. Assert: assertEquals("expected", captor.getValue());
Click to reveal answer
intermediate
Can argument captors capture multiple arguments from multiple calls?
Yes, argument captors can capture all arguments from multiple calls using captor.getAllValues(), which returns a list of all captured arguments in order.
Click to reveal answer
What is the main purpose of an argument captor in unit testing?
✗ Incorrect
Argument captors are used to capture the actual arguments passed to mock methods so you can check their values.
Which Mockito annotation is used to declare an argument captor?
✗ Incorrect
The @Captor annotation declares an argument captor in Mockito.
How do you retrieve all captured arguments from an argument captor?
✗ Incorrect
captor.getAllValues() returns a list of all arguments captured from multiple calls.
Which of the following is NOT a benefit of using argument captors?
✗ Incorrect
Verifying method call count is done by verify(mock, times(n)), not by argument captors.
In Mockito, after capturing an argument, how do you verify the method was called with that argument?
✗ Incorrect
You use verify(mock).method(captor.capture()) to capture the argument during verification.
Explain what an argument captor is and why it is useful in unit testing with mocks.
Think about how you check what data a mock method received.
You got /3 concepts.
Describe the steps to use an argument captor in a JUnit test with Mockito.
Consider the flow from setup to assertion.
You got /4 concepts.