Consider the following JUnit test snippet using Mockito's ArgumentCaptor. What will be the value of captor.getValue() after the test runs?
import static org.mockito.Mockito.*; import org.mockito.ArgumentCaptor; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; interface Service { void process(String input); } public class TestClass { @Test void testArgumentCaptor() { Service mockService = mock(Service.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); mockService.process("hello"); mockService.process("world"); verify(mockService, times(2)).process(captor.capture()); String captured = captor.getValue(); assertEquals("world", captured); } }
Remember that getValue() returns the last captured argument.
The ArgumentCaptor captures all arguments passed to the mocked method. The getValue() method returns the last captured argument, which is "world" in this case.
You have captured multiple arguments with an ArgumentCaptor. Which assertion correctly checks that the captured arguments are exactly ["one", "two", "three"] in order?
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); // assume mock.method called 3 times with "one", "two", "three" verify(mock, times(3)).method(captor.capture()); List<String> capturedArgs = captor.getAllValues();
Check both order and content of the list.
assertEquals with List.of(...) checks both order and exact elements. containsAll only checks presence, not order. Size check alone is insufficient. getValue() returns only the last argument.
Examine the test code below. Why does the verify statement fail with a "Wanted but not invoked" error?
import static org.mockito.Mockito.*; import org.mockito.ArgumentCaptor; import org.junit.jupiter.api.Test; interface Service { void execute(int value); } public class TestClass { @Test void testCaptorFail() { Service mockService = mock(Service.class); ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class); // No call to mockService.execute here verify(mockService).execute(captor.capture()); } }
Check if the mocked method was called before verification.
The verify statement expects the method execute to have been called at least once. Since there is no call to execute on the mock, verify fails with "Wanted but not invoked".
Why do testers use ArgumentCaptor in unit tests with mocks?
Think about what information ArgumentCaptor provides beyond simple call verification.
ArgumentCaptor allows testers to capture the actual arguments passed to mocked methods so they can assert on their values, enabling more detailed and precise testing.
Given a mock List<String> and calls mockList.add("a"), mockList.add("b"), mockList.add("c"), which code snippet correctly captures all arguments and asserts their order?
import static org.mockito.Mockito.*; import org.mockito.ArgumentCaptor; import static org.junit.jupiter.api.Assertions.*; import java.util.List; List<String> mockList = mock(List.class); mockList.add("a"); mockList.add("b"); mockList.add("c");
Check the verify call and how many times add() is expected to be called.
Option A correctly verifies that add() was called 3 times and captures all arguments. Then it asserts the exact order of captured arguments. Option A verifies only once, so it misses calls. Option A uses anyString() in verify, which doesn't capture arguments. Option A asserts only the last captured argument, not all.