0
0
JUnittesting~20 mins

Argument matchers (any, eq) in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Argument Matcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
Anull
Belement
CIndexOutOfBoundsException
DMockitoException
Attempts:
2 left
💡 Hint
Think about what anyInt() matcher does in Mockito stubbing.
assertion
intermediate
2: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?
    }
}
Averify(mockedList).add(anyString());
Bverify(mockedList).add(any());
Cverify(mockedList).add("test");
Dverify(mockedList).add(eq("test"));
Attempts:
2 left
💡 Hint
eq() matches the exact argument value.
🔧 Debug
advanced
2: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);
    }
}
AThe code runs fine and prints "any".
BThe code runs fine and prints "zero".
CMixing eq() and anyInt() in stubbing causes InvalidUseOfMatchersException.
DThe code throws NullPointerException at runtime.
Attempts:
2 left
💡 Hint
Mockito uses the last matching stubbing for the same method signature.
🧠 Conceptual
advanced
2:00remaining
Understanding argument matcher limitations
Which statement about Mockito argument matchers is TRUE?
AYou can mix raw values and argument matchers freely in the same method call.
BArgument matchers only work with primitive types, not objects.
CAll arguments in a method call must be provided by matchers if any matcher is used.
DArgument matchers can be used only in verify(), not in when() stubbing.
Attempts:
2 left
💡 Hint
Mockito requires consistency in argument matching.
framework
expert
3: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?
AImplement ArgumentMatcher<T> and use argThat() with your custom matcher.
BUse any() matcher to accept any object of that type.
CUse raw object comparison without any matcher.
DUse eq() matcher with the exact object instance.
Attempts:
2 left
💡 Hint
Custom logic requires custom matcher implementation.