Recall & Review
beginner
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?✗ Incorrect
Using
never() verifies that the method was not called at all.Which verification checks if a method was called at least 3 times?
✗ 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?✗ 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?
✗ 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?
✗ 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.