We use verification times to check how many times a method was called in a test. This helps us make sure our code behaves as expected.
Verification times (times, never, atLeast) in JUnit
verify(mockObject, times(n)).methodCall(); verify(mockObject, never()).methodCall(); verify(mockObject, atLeast(n)).methodCall();
verify checks the method call on the mock object.
times(n) means exactly n times, never() means zero times, and atLeast(n) means n or more times.
add("item") was called exactly twice on mockList.verify(mockList, times(2)).add("item");
clear() was never called on mockList.verify(mockList, never()).clear();
add("item") was called at least once on mockList.verify(mockList, atLeast(1)).add("item");
This test creates a mock list and calls add("apple") twice. It then verifies the method calls using times(2), never(), and atLeast(1). If all verifications pass, it prints a success message.
import static org.mockito.Mockito.*; import java.util.List; public class VerificationTimesTest { public static void main(String[] args) { List<String> mockList = mock(List.class); mockList.add("apple"); mockList.add("apple"); // Verify add("apple") called exactly 2 times verify(mockList, times(2)).add("apple"); // Verify clear() was never called verify(mockList, never()).clear(); // Verify add("apple") called at least once verify(mockList, atLeast(1)).add("apple"); System.out.println("All verifications passed."); } }
If a verification fails, Mockito throws an exception and the test fails.
Use never() to ensure no unwanted calls happen.
atLeast(n) is useful when you expect multiple calls but exact count is not fixed.
Verification times help check how often methods are called in tests.
times(n) means exactly n calls, never() means zero calls, and atLeast(n) means n or more calls.
These checks make tests more precise and reliable.
Practice
Mockito verification method checks that a method was called exactly 3 times?Solution
Step 1: Understand
Thetimes(n)usagetimes(n)method verifies that the method was called exactlyntimes.Step 2: Match the exact call count
Since the question asks for exactly 3 calls,times(3)is the correct choice.Final Answer:
verify(mock, times(3)).method()-> Option DQuick Check:
Exact call count = times(n) [OK]
- Confusing times(n) with atLeast(n)
- Using never() when calls exist
- Using atMost(n) instead of times(n)
Solution
Step 1: Identify the correct method for zero calls
Mockito providesnever()to verify that a method was not called at all.Step 2: Check syntax correctness
The correct syntax isverify(mock, never()).method();. Usingtimes(0)also works butnever()is clearer and preferred.Final Answer:
verify(mock, never()).method(); -> Option AQuick Check:
No calls = never() [OK]
- Using times(0) instead of never() for clarity
- Incorrect chaining like verify(mock).never()
- Using atLeast(0) which always passes
verify(mock, atLeast(2)).process();
Which statement is true about the verification?
Solution
Step 1: Understand
TheatLeast(n)meaningatLeast(n)method verifies that the method was called at leastntimes, meaning 2 or more times.Step 2: Match the verification condition
Since the code usesatLeast(2), it means the method must be called 2 or more times, not exactly 2.Final Answer:
The methodprocess()must be called at least 2 times or more. -> Option CQuick Check:
Minimum calls = atLeast(n) [OK]
- Confusing atLeast(n) with times(n)
- Thinking atLeast(n) means at most n
- Assuming never() means zero calls only
verify(mock, never).execute();
Solution
Step 1: Check syntax of never()
Thenever()method requires parentheses to be called as a method.Step 2: Identify missing parentheses
The code usesneverwithout parentheses, which is a syntax error.Final Answer:
Missing parentheses after never() -> Option BQuick Check:
never() requires parentheses [OK]
- Omitting parentheses after never
- Confusing never with times(0)
- Assuming method name causes error
update() was called at least once but no more than 5 times. Which Mockito verification code correctly expresses this?Solution
Step 1: Understand the requirement
The methodupdate()must be called at least once and at most 5 times.Step 2: Use correct verification methods
UseatLeast(1)to check minimum calls andatMost(5)to check maximum calls.Step 3: Combine verifications
Both verifications must be done separately to ensure the call count is between 1 and 5.Final Answer:
verify(mock, atLeast(1)).update(); verify(mock, atMost(5)).update(); -> Option AQuick Check:
Range check = atLeast + atMost [OK]
- Using times() twice with different values
- Using never() when calls exist
- Using only atLeast(5) which ignores max limit
Ready to prove it?
Pass the test and this pattern moves unknown
