Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to verify that the method save() was called exactly once on the mock object.
JUnit
verify(mockObject).[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that was never called on the mock.
Forgetting to call the method inside
verify().✗ Incorrect
The
verify() method checks that save() was called on the mock object. Only option D matches the method name to verify.2fill in blank
mediumComplete the code to verify that the method delete() was never called on the mock object.
JUnit
verify(mockObject, [1]()).delete(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
times(0) instead of never() (both work but never() is clearer).Using
atLeastOnce() which means the method was called at least once.✗ Incorrect
Using
never() verifies that the method was not called at all. This matches the requirement.3fill in blank
hardFix the error in the code to verify that update() was called exactly twice on the mock object.
JUnit
verify(mockObject, [1](2)).update();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
never() which means zero calls.Using
atLeast(2) which means two or more calls.✗ Incorrect
The
times(2) mode verifies the method was called exactly two times.4fill in blank
hardFill both blanks to verify that the method process() was called at least once but no more than three times on the mock object.
JUnit
verify(mockObject, [1](1)).process(); verify(mockObject, [2](3)).process();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
times() for range checks instead of atLeast and atMost.Using
never() which means zero calls.✗ Incorrect
Use
atLeast(1) to check minimum calls and atMost(3) to check maximum calls.5fill in blank
hardFill all three blanks to demonstrate verifications that the method calculate() was called exactly once, never called, and at least twice.
JUnit
verify(mockObject, [1](1)).calculate(); verify(mockObject, [2]()).calculate(); verify(mockObject, [3](2)).calculate();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up
atMost and atLeast.Using
times() without a number.✗ Incorrect
First verify exactly once with
times(1), verify never called with never(), and finally verify at least twice with atLeast(2).