0
0
JUnittesting~20 mins

Argument captors in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Argument Captor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this JUnit test using ArgumentCaptor?

Consider the following JUnit test snippet using Mockito's ArgumentCaptor. What will be the value of captor.getValue() after the test runs?

JUnit
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);
    }
}
A"hello"
B"world"
Cnull
DThrows an exception
Attempts:
2 left
💡 Hint

Remember that getValue() returns the last captured argument.

assertion
intermediate
2:00remaining
Which assertion correctly verifies all captured arguments?

You have captured multiple arguments with an ArgumentCaptor. Which assertion correctly checks that the captured arguments are exactly ["one", "two", "three"] in order?

JUnit
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();
AassertEquals("three", captor.getValue());
BassertTrue(capturedArgs.containsAll(List.of("one", "two", "three")));
CassertEquals(3, capturedArgs.size());
DassertEquals(List.of("one", "two", "three"), capturedArgs);
Attempts:
2 left
💡 Hint

Check both order and content of the list.

🔧 Debug
advanced
2:00remaining
Why does this ArgumentCaptor test fail with "Wanted but not invoked" error?

Examine the test code below. Why does the verify statement fail with a "Wanted but not invoked" error?

JUnit
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());
    }
}
ABecause verify requires times(0) when no calls are made.
BBecause ArgumentCaptor cannot capture primitive types like int.
CBecause the method execute was never called on the mock, so verify fails.
DBecause captor.capture() must be called before verify.
Attempts:
2 left
💡 Hint

Check if the mocked method was called before verification.

🧠 Conceptual
advanced
1:30remaining
What is the main advantage of using ArgumentCaptor in tests?

Why do testers use ArgumentCaptor in unit tests with mocks?

ATo capture and assert the arguments passed to mocked methods for detailed verification.
BTo replace the need for mocking dependencies entirely.
CTo automatically generate test data for parameterized tests.
DTo speed up test execution by caching method calls.
Attempts:
2 left
💡 Hint

Think about what information ArgumentCaptor provides beyond simple call verification.

framework
expert
2:30remaining
Which Mockito code snippet correctly captures and asserts multiple arguments in order?

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?

JUnit
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");
A
ArgumentCaptor&lt;String&gt; captor = ArgumentCaptor.forClass(String.class);
verify(mockList, times(3)).add(captor.capture());
assertEquals(List.of("a", "b", "c"), captor.getAllValues());
B
ArgumentCaptor&lt;String&gt; captor = ArgumentCaptor.forClass(String.class);
verify(mockList).add(captor.capture());
assertEquals(List.of("a", "b", "c"), captor.getAllValues());
C
ArgumentCaptor&lt;String&gt; captor = ArgumentCaptor.forClass(String.class);
verify(mockList, times(3)).add(anyString());
assertEquals(List.of("a", "b", "c"), captor.getAllValues());
D
ArgumentCaptor&lt;String&gt; captor = ArgumentCaptor.forClass(String.class);
verify(mockList, times(3)).add(captor.capture());
assertEquals("c", captor.getValue());
Attempts:
2 left
💡 Hint

Check the verify call and how many times add() is expected to be called.