0
0
JUnittesting~20 mins

Verification times (times, never, atLeast) in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Verification Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
ATest fails because add("test") was called only once.
BTest passes because add("test") was called exactly twice.
CTest fails because times(2) is not a valid verification mode.
DTest passes because add("test") was called at least twice.
Attempts:
2 left
💡 Hint
Remember that times(2) means exactly two calls.
Predict Output
intermediate
2: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();
ATest passes because clear() was never called.
BTest fails because add("hello") was called.
CTest fails because never() requires at least one call.
DTest passes because add("hello") counts as clear() call.
Attempts:
2 left
💡 Hint
never() means the method should not be called at all.
assertion
advanced
2: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();
Averify(mockService, times(1)).process();
Bverify(mockService).process();
Cverify(mockService, never()).process();
Dverify(mockService, atLeast(2)).process();
Attempts:
2 left
💡 Hint
atLeast(2) means two or more calls are expected.
🔧 Debug
advanced
2: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();
Atimes(0) is valid and means the method was never called.
Btimes(0) means the method was called once.
Ctimes(0) causes a runtime exception.
Dtimes(0) is invalid; use never() instead.
Attempts:
2 left
💡 Hint
times(0) and never() are equivalent in Mockito.
framework
expert
2: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?
Anever()
Btimes(1)
CatLeastOnce()
Dtimes(0)
Attempts:
2 left
💡 Hint
Think about a verification mode that allows one or more calls.