0
0
JUnittesting~5 mins

Verification times (times, never, atLeast) in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe method was called at least once
BThe method was called exactly once
CThe method was never called
DThe method was called multiple times
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))
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
Which verification is used to confirm no interaction with a mock method?
Anever()
Btimes(0)
CatLeast(1)
Dtimes(1)
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))
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.