What does verify(mock, times(n)) do in JUnit testing?
It checks that the mock method was called exactly n times during the test execution.
Click to reveal answer
beginner
Explain the use of verify(mock, never()).
It verifies that the mock method was never called during the test. This ensures no interaction happened with that method.
Click to reveal answer
intermediate
What does verify(mock, atLeast(n)) check?
It checks that the mock method was called at least n times, meaning n or more times.
Click to reveal answer
beginner
Why is verification important in unit testing with mocks?
Verification confirms that the code interacts with dependencies as expected, ensuring correct behavior and catching unexpected calls.
Click to reveal answer
beginner
How would you verify a method was called exactly once using JUnit and Mockito?
Use verify(mock, times(1)).methodName() to check the method was called exactly one time.
Click to reveal answer
What does verify(mock, never()).someMethod() ensure?
AThe method was called at least once
BThe method was called exactly once
CThe method was never called
DThe method was called multiple times
✗ Incorrect
Using never() verifies that the method was not called at all.
Which verification checks if a method was called at least 3 times?
Averify(mock, times(3))
Bverify(mock, never())
Cverify(mock, atMost(3))
Dverify(mock, atLeast(3))
✗ Incorrect
atLeast(3) verifies the method was called 3 or more times.
What happens if verify(mock, times(2)) is used but the method was called only once?
ATest passes
BTest fails with verification error
CTest ignores verification
DTest retries the method call
✗ Incorrect
The test fails because the method was not called the expected number of times.
Which verification is used to confirm no interaction with a mock method?
Anever()
Btimes(0)
CatLeast(1)
Dtimes(1)
✗ Incorrect
never() explicitly checks that the method was never called.
If you want to verify a method was called exactly once, which is the best choice?
Averify(mock, times(1))
Bverify(mock, atLeast(1))
Cverify(mock, never())
Dverify(mock, atMost(1))
✗ Incorrect
times(1) ensures the method was called exactly one time.
Describe how you would verify method call counts using JUnit and Mockito.
Think about how to check if a method was called zero, exactly, or at least a number of times.
You got /4 concepts.
Explain why verifying method calls is important in unit tests with mocks.
Consider what happens if dependencies are called incorrectly or unexpectedly.
You got /4 concepts.
Practice
(1/5)
1. Which Mockito verification method checks that a method was called exactly 3 times?
easy
A. verify(mock, atMost(3)).method()
B. verify(mock, never()).method()
C. verify(mock, atLeast(3)).method()
D. verify(mock, times(3)).method()
Solution
Step 1: Understand times(n) usage
The times(n) method verifies that the method was called exactly n times.
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 D
Quick Check:
Exact call count = times(n) [OK]
Hint: Use times(n) for exact call count verification [OK]
Common Mistakes:
Confusing times(n) with atLeast(n)
Using never() when calls exist
Using atMost(n) instead of times(n)
2. Which of the following is the correct syntax to verify that a method was never called using Mockito?
easy
A. verify(mock, never()).method();
B. verify(mock, atLeast(0)).method();
C. verify(mock, times(0));
D. verify(mock).never().method();
Solution
Step 1: Identify the correct method for zero calls
Mockito provides never() to verify that a method was not called at all.
Step 2: Check syntax correctness
The correct syntax is verify(mock, never()).method();. Using times(0) also works but never() is clearer and preferred.
Final Answer:
verify(mock, never()).method(); -> Option A
Quick Check:
No calls = never() [OK]
Hint: Use never() to verify zero calls clearly [OK]
Common Mistakes:
Using times(0) instead of never() for clarity
Incorrect chaining like verify(mock).never()
Using atLeast(0) which always passes
3. Given the following code snippet:
verify(mock, atLeast(2)).process();
Which statement is true about the verification?
medium
A. The method process() must be called exactly 2 times.
B. The method process() must be called at most 2 times.
C. The method process() must be called at least 2 times or more.
D. The method process() must never be called.
Solution
Step 1: Understand atLeast(n) meaning
The atLeast(n) method verifies that the method was called at leastn times, meaning 2 or more times.
Step 2: Match the verification condition
Since the code uses atLeast(2), it means the method must be called 2 or more times, not exactly 2.
Final Answer:
The method process() must be called at least 2 times or more. -> Option C
Quick Check:
Minimum calls = atLeast(n) [OK]
Hint: atLeast(n) means minimum n calls, not exact [OK]
Common Mistakes:
Confusing atLeast(n) with times(n)
Thinking atLeast(n) means at most n
Assuming never() means zero calls only
4. Identify the error in this Mockito verification:
verify(mock, never).execute();
medium
A. Using times() instead of never()
B. Missing parentheses after never()
C. Incorrect method name execute()
D. No error, syntax is correct
Solution
Step 1: Check syntax of never()
The never() method requires parentheses to be called as a method.
Step 2: Identify missing parentheses
The code uses never without parentheses, which is a syntax error.
Final Answer:
Missing parentheses after never() -> Option B
Quick Check:
never() requires parentheses [OK]
Hint: Always add () after never to avoid syntax errors [OK]
Common Mistakes:
Omitting parentheses after never
Confusing never with times(0)
Assuming method name causes error
5. You want to verify that a method update() was called at least once but no more than 5 times. Which Mockito verification code correctly expresses this?
hard
A. verify(mock, atLeast(1)).update(); verify(mock, atMost(5)).update();
B. verify(mock, times(1)).update(); verify(mock, times(5)).update();
C. verify(mock, never()).update();
D. verify(mock, atLeast(5)).update();
Solution
Step 1: Understand the requirement
The method update() must be called at least once and at most 5 times.
Step 2: Use correct verification methods
Use atLeast(1) to check minimum calls and atMost(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 A
Quick Check:
Range check = atLeast + atMost [OK]
Hint: Combine atLeast(n) and atMost(m) for call count range [OK]
Common Mistakes:
Using times() twice with different values
Using never() when calls exist
Using only atLeast(5) which ignores max limit
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.