Challenge - 5 Problems
Argument Matcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Mockito argument matcher usage
What will be the output of the following JUnit test using Mockito argument matchers?
JUnit
import static org.mockito.Mockito.*; import static org.mockito.ArgumentMatchers.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import java.util.List; class MyTest { @Test void testArgumentMatchers() { List<String> mockedList = mock(List.class); when(mockedList.get(anyInt())).thenReturn("element"); String result = mockedList.get(5); assertEquals("element", result); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Think about what anyInt() matcher does in Mockito stubbing.
✗ Incorrect
The anyInt() matcher matches any integer argument, so mockedList.get(5) returns the stubbed value "element".
❓ assertion
intermediate2:00remaining
Correct assertion with eq matcher
Which assertion correctly verifies that a mocked method was called with the exact string "test" using Mockito argument matchers?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import java.util.List; class MyTest { @Test void verifyExactArgument() { List<String> mockedList = mock(List.class); mockedList.add("test"); // Which verify statement is correct? } }
Attempts:
2 left
💡 Hint
eq() matches the exact argument value.
✗ Incorrect
Using eq("test") ensures the method was called with exactly "test". Other options either match any argument or do not use matchers properly.
🔧 Debug
advanced2:00remaining
Identify the error in argument matcher usage
What is the error in the following Mockito test code snippet?
JUnit
import static org.mockito.Mockito.*; import static org.mockito.ArgumentMatchers.*; import org.junit.jupiter.api.Test; import java.util.List; class MyTest { @Test void testIncorrectMatcher() { List<String> mockedList = mock(List.class); when(mockedList.get(eq(0))).thenReturn("zero"); when(mockedList.get(anyInt())).thenReturn("any"); String result = mockedList.get(0); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Mockito uses the last matching stubbing for the same method signature.
✗ Incorrect
The last stubbing with anyInt() overrides the previous one with eq(0), so mockedList.get(0) returns "any".
🧠 Conceptual
advanced2:00remaining
Understanding argument matcher limitations
Which statement about Mockito argument matchers is TRUE?
Attempts:
2 left
💡 Hint
Mockito requires consistency in argument matching.
✗ Incorrect
If you use any argument matcher in a method call, all arguments must be provided by matchers to avoid errors.
❓ framework
expert3:00remaining
Best practice for custom argument matcher implementation
You want to verify a method call with a complex object argument using a custom condition in Mockito. Which approach is best to implement this?
Attempts:
2 left
💡 Hint
Custom logic requires custom matcher implementation.
✗ Incorrect
Implementing ArgumentMatcher and using argThat() allows you to define complex matching logic for arguments.