Challenge - 5 Problems
Verification Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this JUnit verification?
Given the following Mockito verification code, what will be the test result?
JUnit
mockList.add("test"); mockList.add("test"); verify(mockList, times(2)).add("test");
Attempts:
2 left
💡 Hint
Remember that times(2) means exactly two calls.
✗ Incorrect
The verify statement expects exactly two calls to add("test"). Since the method is called twice, the verification passes.
❓ Predict Output
intermediate2:00remaining
What happens if a method is never called but verified with never()?
Consider this code snippet:
mockList.add("hello");
verify(mockList, never()).clear();
JUnit
mockList.add("hello");
verify(mockList, never()).clear();Attempts:
2 left
💡 Hint
never() means the method should not be called at all.
✗ Incorrect
The verify statement checks that clear() was never called. Since clear() was not called, the verification passes.
❓ assertion
advanced2:00remaining
Which verification will fail if the method is called once?
Assume the method mockService.process() is called exactly once. Which verify statement will fail?
JUnit
mockService.process();
Attempts:
2 left
💡 Hint
atLeast(2) means two or more calls are expected.
✗ Incorrect
Since process() is called only once, atLeast(2) verification fails because it expects two or more calls.
🔧 Debug
advanced2:00remaining
Identify the error in this verification code
What is wrong with this verification code snippet?
verify(mockObj, times(0)).execute();
JUnit
verify(mockObj, times(0)).execute();Attempts:
2 left
💡 Hint
times(0) and never() are equivalent in Mockito.
✗ Incorrect
times(0) is a valid verification mode meaning the method was not called. It is equivalent to never().
❓ framework
expert2:00remaining
Which verification mode best fits this scenario?
You want to verify that a method was called at least once but you don't care how many times exactly. Which verification mode should you use?
Attempts:
2 left
💡 Hint
Think about a verification mode that allows one or more calls.
✗ Incorrect
atLeastOnce() verifies the method was called one or more times, matching the scenario.