What is the purpose of argument matchers like any() and eq() in JUnit testing?
Argument matchers help specify flexible or exact values when verifying or stubbing method calls in tests. any() matches any value of the given type, while eq() matches an exact value.
Click to reveal answer
beginner
How does any() differ from eq() in argument matching?
any() accepts any argument of the specified type without checking its value. eq() requires the argument to exactly match the given value.
Click to reveal answer
beginner
Show a simple example of using eq() in a JUnit test to verify a method call.
Example:
verify(mockObject).someMethod(eq("hello"));
This checks that someMethod was called with the exact string "hello".
Click to reveal answer
intermediate
Why should you avoid mixing raw values and argument matchers in the same method call verification?
Mixing raw values and argument matchers causes errors because the mocking framework expects either all arguments to be matchers or none. Use eq() for exact values to avoid this.
Click to reveal answer
intermediate
What happens if you use any() without specifying the type in JUnit argument matching?
You must specify the type with any(), like any(String.class). Omitting the type causes compilation errors because the matcher needs to know the argument type.
Click to reveal answer
What does eq(5) do in a JUnit mock verification?
AMatches exactly the integer 5
BMatches any argument except 5
CMatches any integer argument
DThrows an error
✗ Incorrect
eq(5) matches only the exact integer value 5.
Which argument matcher would you use to accept any String argument in a method call?
Aeq(String.class)
Bany()
Cany(String.class)
Deq(any())
✗ Incorrect
any(String.class) matches any String argument.
Why is mixing raw values and argument matchers in the same method call verification problematic?
AIt causes compilation or runtime errors
BIt causes the test to pass always
CIt improves test readability
DIt is recommended for flexibility
✗ Incorrect
Mixing raw values and matchers causes errors because the mocking framework expects consistency.
What will happen if you write any() without a type in JUnit 5?
AIt matches only primitive types
BIt matches any argument of any type
CIt matches only null values
DIt causes a compilation error
✗ Incorrect
any() requires a type parameter like any(String.class) to compile.
Which of the following is a correct way to verify a method call with any integer argument?
Averify(mock).method(eq(5));
Bverify(mock).method(any(Integer.class));
Cverify(mock).method(eq(any()));
Dverify(mock).method(any());
✗ Incorrect
any(Integer.class) correctly matches any Integer argument.
Explain the difference between any() and eq() argument matchers in JUnit.
Think about flexible vs exact matching.
You got /4 concepts.
Describe best practices when using argument matchers in JUnit tests.
Consider consistency and type safety.
You got /4 concepts.
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
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.
Step 2: Compare with other matchers
Unlike eq(), which matches exact values, any() is flexible and matches all values of the specified type.
Final Answer:
It matches any value of the specified type. -> Option A
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
Step 1: Identify correct use of eq()
The eq() matcher is used to match an exact argument value in Mockito verification.
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".
Final Answer:
verify(mock).method(eq("test")); -> Option C
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
Step 1: Analyze the stubbing with any()
The when clause uses any(String.class) to match any string argument and returns "done".
Step 2: Check the method call argument
The method is called with "input", which matches any(String.class), so the stub returns "done".
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
Step 1: Check usage of any()
The any() matcher requires a type parameter like any(String.class) to specify the argument type.
Step 2: Confirm correct matcher usage
Without the type, Mockito cannot determine the argument type, causing a compilation error.
Final Answer:
Missing type parameter in any() matcher. -> Option B
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
Step 1: Match exact first argument
The first argument must be exactly "test", so use eq("test").
Step 2: Match any Integer second argument
The second argument can be any Integer, so use any(Integer.class).
Step 3: Verify combined usage
verify(mock).method(eq("test"), any(Integer.class)); correctly combines eq() and any() for the two arguments.
Final Answer:
verify(mock).method(eq("test"), any(Integer.class)); -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.