0
0
JUnittesting~5 mins

verify() for interaction verification in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of verify() in interaction verification?

verify() checks if a specific method was called on a mock object during the test. It helps confirm that the expected interactions happened.

Click to reveal answer
beginner
How do you use verify() to check a method was called exactly once?

Use verify(mockObject).methodName(args); to check the method was called once.

Click to reveal answer
beginner
What happens if verify() fails during a test?

The test fails with an error showing that the expected method call did not happen as specified.

Click to reveal answer
intermediate
How can you verify a method was never called?

Use verify(mockObject, never()).methodName(args); to check the method was not called at all.

Click to reveal answer
intermediate
Explain the difference between verify() and verifyNoMoreInteractions().

verify() checks specific method calls, while verifyNoMoreInteractions() ensures no other calls happened on the mock beyond those verified.

Click to reveal answer
What does verify(mock).someMethod() check in a test?
AThat <code>someMethod()</code> was never called
BThat <code>mock</code> is not null
CThat <code>someMethod()</code> returned true
DThat <code>someMethod()</code> was called on <code>mock</code> exactly once
How do you verify a method was called exactly twice?
Averify(mock).method();
Bverify(mock, once()).method();
Cverify(mock, times(2)).method();
Dverify(mock, never()).method();
What is the result if verify() does not find the expected method call?
ATest passes silently
BTest fails with an error
CTest skips the verification
DTest logs a warning but passes
Which method verifies no other interactions happened on a mock?
AverifyNoMoreInteractions(mock);
Bverify(mock);
Cverify(mock, never());
DverifyZeroInteractions(mock);
To check a method was never called, which syntax is correct?
Averify(mock, never()).method();
Bverify(mock).method();
Cverify(mock, times(0)).method();
DverifyNoMoreInteractions(mock);
Describe how verify() helps in testing interactions with mock objects.
Think about confirming what your code did with the mock.
You got /3 concepts.
    Explain the difference between verifying a method call and verifying no more interactions.
    One is about specific calls, the other about unexpected calls.
    You got /3 concepts.