What if you could write fewer tests but catch more mistakes by matching arguments smartly?
Why Argument matchers (any, eq) in JUnit? - Purpose & Use Cases
Imagine you have a method that calls another method with different inputs, and you want to check if it was called correctly. Doing this by hand means writing many tests for every possible input value.
You have to write separate checks for each input, which is tiring and easy to forget.
Manually checking every argument value is slow and error-prone. You might miss some cases or write repetitive code that is hard to maintain.
Also, if the input changes slightly, you must rewrite many tests, making the process painful.
Argument matchers like any() and eq() let you write flexible tests that focus on what matters.
You can say "I don't care what the argument is" with any(), or "It must equal this value" with eq(). This makes tests simpler and easier to read.
verify(mock).method("exactValue"); verify(mock).method("anotherValue");
verify(mock).method(eq("exactValue"));
verify(mock).method(any());It enables writing clear, concise tests that focus on important argument values without getting stuck on details.
When testing a service that sends emails, you can check if the send method was called with any email address, or exactly the one you expect, without writing many tests for each address.
Manual argument checks are slow and repetitive.
Argument matchers simplify tests by focusing on key values.
They make tests easier to write, read, and maintain.
Practice
any() argument matcher do in JUnit Mockito tests?Solution
Step 1: Understand the purpose of
Theany()any()matcher is used to accept any argument of a given type in method verification or stubbing.Step 2: Compare with other matchers
Unlikeeq(), 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 AQuick Check:
any()= matches any value [OK]
- Confusing any() with eq()
- Thinking any() matches only null
- Assuming any() matches only specific values
Solution
Step 1: Identify correct use of eq()
Theeq()matcher is used to match an exact argument value in Mockito verification.Step 2: Check syntax correctness
verify(mock).method(eq("test")); correctly useseq("test")to verify the method was called with the exact string "test".Final Answer:
verify(mock).method(eq("test")); -> Option CQuick Check:
Exact value match = eq() [OK]
- Using any() when exact match is needed
- Omitting eq() for exact value
- Using any() without specifying type
when(service.process(any(String.class))).thenReturn("done");
String result = service.process("input");What will be the value of
result after execution?Solution
Step 1: Analyze the stubbing with any()
Thewhenclause usesany(String.class)to match any string argument and returns "done".Step 2: Check the method call argument
The method is called with "input", which matchesany(String.class), so the stub returns "done".Final Answer:
"done" -> Option DQuick Check:
any(String.class) matches "input" = returns "done" [OK]
- Assuming method returns input argument
- Thinking any() causes exception
- Ignoring stubbing behavior
verify(mock).update(eq("data"), any());Solution
Step 1: Check usage of any()
Theany()matcher requires a type parameter likeany(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 BQuick Check:
any() needs type like any(String.class) [OK]
- Using any() without type parameter
- Mixing eq() and any() incorrectly
- Assuming verify() disallows matchers
Solution
Step 1: Match exact first argument
The first argument must be exactly "test", so useeq("test").Step 2: Match any Integer second argument
The second argument can be any Integer, so useany(Integer.class).Step 3: Verify combined usage
verify(mock).method(eq("test"), any(Integer.class)); correctly combineseq()andany()for the two arguments.Final Answer:
verify(mock).method(eq("test"), any(Integer.class)); -> Option AQuick Check:
eq() + any(type) = exact + any argument [OK]
- Swapping argument matchers incorrectly
- Using any() without type
- Using eq() for flexible arguments
Ready to prove it?
Pass the test and this pattern moves unknown
