What if your tests could instantly tell you if a method was called too many or too few times, without you counting?
Why Verification times (times, never, atLeast) in JUnit? - Purpose & Use Cases
Imagine you have a simple app where a button click should call a method exactly three times. You try to check this by watching the screen and counting clicks yourself.
Counting method calls manually is slow and easy to mess up. You might lose track, especially if calls happen fast or in loops. This leads to mistakes and wasted time.
Verification times in JUnit let you automatically check how many times a method was called. You can say: call must happen exactly 3 times, never, or at least once. This saves time and avoids errors.
if (callCount == 3) { pass; } else { fail; }
verify(mock, times(3)).method();You can trust your tests to catch wrong call counts quickly and clearly, making your code more reliable.
In a shopping app, you want to check that the payment process calls the 'chargeCard' method exactly once per purchase, never more or less.
Manual counting of method calls is error-prone and slow.
Verification times automate checking call counts in tests.
This makes tests clearer, faster, and more 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
