Bird
Raised Fist0
JUnittesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the any() argument matcher do in JUnit Mockito tests?
easy
A. It matches any value of the specified type.
B. It matches only null values.
C. It matches only exact values.
D. It matches values that are greater than zero.

Solution

  1. Step 1: Understand the purpose of any()

    The any() matcher is used to accept any argument of a given type in method verification or stubbing.
  2. Step 2: Compare with other matchers

    Unlike eq(), which matches exact values, any() is flexible and matches all values of the specified type.
  3. Final Answer:

    It matches any value of the specified type. -> Option A
  4. Quick Check:

    any() = matches any value [OK]
Hint: Remember: any() accepts all values of a type [OK]
Common Mistakes:
  • Confusing any() with eq()
  • Thinking any() matches only null
  • Assuming any() matches only specific values
2. Which of the following is the correct syntax to verify a method call with an exact argument value using an argument matcher in Mockito?
easy
A. verify(mock).method(any(String.class));
B. verify(mock).method(any());
C. verify(mock).method(eq("test"));
D. verify(mock).method("test");

Solution

  1. Step 1: Identify correct use of eq()

    The eq() matcher is used to match an exact argument value in Mockito verification.
  2. Step 2: Check syntax correctness

    verify(mock).method(eq("test")); correctly uses eq("test") to verify the method was called with the exact string "test".
  3. Final Answer:

    verify(mock).method(eq("test")); -> Option C
  4. Quick Check:

    Exact value match = eq() [OK]
Hint: Use eq() to match exact argument values [OK]
Common Mistakes:
  • Using any() when exact match is needed
  • Omitting eq() for exact value
  • Using any() without specifying type
3. Consider the following code snippet:
when(service.process(any(String.class))).thenReturn("done");
String result = service.process("input");

What will be the value of result after execution?
medium
A. "input"
B. null
C. Throws an exception
D. "done"

Solution

  1. Step 1: Analyze the stubbing with any()

    The when clause uses any(String.class) to match any string argument and returns "done".
  2. Step 2: Check the method call argument

    The method is called with "input", which matches any(String.class), so the stub returns "done".
  3. Final Answer:

    "done" -> Option D
  4. Quick Check:

    any(String.class) matches "input" = returns "done" [OK]
Hint: any() matches all inputs, so stub returns specified value [OK]
Common Mistakes:
  • Assuming method returns input argument
  • Thinking any() causes exception
  • Ignoring stubbing behavior
4. Identify the error in the following Mockito verification code:
verify(mock).update(eq("data"), any());
medium
A. eq() cannot be used with any().
B. Missing type parameter in any() matcher.
C. verify() should not be used with matchers.
D. No error; code is correct.

Solution

  1. Step 1: Check usage of any()

    The any() matcher requires a type parameter like any(String.class) to specify the argument type.
  2. Step 2: Confirm correct matcher usage

    Without the type, Mockito cannot determine the argument type, causing a compilation error.
  3. Final Answer:

    Missing type parameter in any() matcher. -> Option B
  4. Quick Check:

    any() needs type like any(String.class) [OK]
Hint: Always specify type in any() like any(String.class) [OK]
Common Mistakes:
  • Using any() without type parameter
  • Mixing eq() and any() incorrectly
  • Assuming verify() disallows matchers
5. You want to verify a method call that takes two arguments: a String and an Integer. The first argument must be exactly "test", and the second can be any Integer. Which Mockito verification is correct?
hard
A. verify(mock).method(eq("test"), any(Integer.class));
B. verify(mock).method(any(String.class), eq(1));
C. verify(mock).method(any(), any());
D. verify(mock).method(eq("test"), eq(1));

Solution

  1. Step 1: Match exact first argument

    The first argument must be exactly "test", so use eq("test").
  2. Step 2: Match any Integer second argument

    The second argument can be any Integer, so use any(Integer.class).
  3. Step 3: Verify combined usage

    verify(mock).method(eq("test"), any(Integer.class)); correctly combines eq() and any() for the two arguments.
  4. Final Answer:

    verify(mock).method(eq("test"), any(Integer.class)); -> Option A
  5. Quick Check:

    eq() + any(type) = exact + any argument [OK]
Hint: Use eq() for exact, any(type) for flexible args [OK]
Common Mistakes:
  • Swapping argument matchers incorrectly
  • Using any() without type
  • Using eq() for flexible arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard