0
0
JUnittesting~5 mins

Argument captors in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ATo capture and inspect arguments passed to mock methods
BTo create mock objects automatically
CTo verify the number of times a method was called
DTo generate test data
Which Mockito annotation is used to declare an argument captor?
A@Mock
B@Spy
C@InjectMocks
D@Captor
How do you retrieve all captured arguments from an argument captor?
Acaptor.getValue()
Bcaptor.getAllValues()
Ccaptor.values()
Dcaptor.captureAll()
Which of the following is NOT a benefit of using argument captors?
AVerifying method call count
BInspecting complex argument values
CCapturing arguments from multiple calls
DAsserting on argument properties
In Mockito, after capturing an argument, how do you verify the method was called with that argument?
Averify(mock).method(captor.getValue())
Bcaptor.verify(mock).method()
Cverify(mock).method(captor.capture())
Dmock.verify(captor.capture())
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.