Challenge - 5 Problems
Mockito Interaction 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 test using Mockito's verify()?
Consider the following JUnit test code snippet using Mockito. What will be the test result after execution?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; class Service { void process() {} } public class VerifyTest { @Test void testVerifyCalledOnce() { Service service = mock(Service.class); service.process(); verify(service).process(); } }
Attempts:
2 left
💡 Hint
Mockito's verify() checks if the method was called exactly once by default.
✗ Incorrect
The test mocks the Service class, calls process() once, then verifies that process() was called exactly once. Since it was called once, the verification passes and the test passes.
❓ assertion
intermediate2:00remaining
Which verify() assertion checks that a method was never called?
In Mockito, which verify() usage correctly asserts that the method process() was never called on the mock?
JUnit
Service service = mock(Service.class);
// no call to service.process() hereAttempts:
2 left
💡 Hint
Check the Mockito documentation for verifying zero invocations.
✗ Incorrect
verify(service, never()).process() asserts that process() was never called. times(0) is equivalent but less readable. verify(service).process() expects one call. verifyNoMoreInteractions checks no other calls beyond verified ones.
🔧 Debug
advanced2:00remaining
Why does this verify() call fail with TooManyActualInvocations?
Given the code below, why does the verify() call fail with a TooManyActualInvocations exception?
JUnit
Service service = mock(Service.class);
service.process();
service.process();
verify(service).process();Attempts:
2 left
💡 Hint
Check how many times process() was called versus what verify() expects.
✗ Incorrect
The method process() was called twice, but verify(service).process() expects exactly one call. This mismatch causes the TooManyActualInvocations exception.
🧠 Conceptual
advanced2:00remaining
What is the purpose of verifyNoMoreInteractions() in Mockito?
Select the best description of what verifyNoMoreInteractions(mock) does in Mockito tests.
Attempts:
2 left
💡 Hint
Think about verifying that no unexpected calls happened.
✗ Incorrect
verifyNoMoreInteractions(mock) ensures that after verifying some interactions, no other calls were made on the mock. It helps catch unexpected method calls.
❓ framework
expert3:00remaining
In a JUnit 5 test using Mockito, which verify() usage correctly checks a method was called exactly twice with specific arguments?
Given a mock of Service with method process(String input), which verify() call correctly asserts process("data") was called exactly twice?
JUnit
Service service = mock(Service.class); service.process("data"); service.process("data");
Attempts:
2 left
💡 Hint
Check how to specify exact call counts with arguments in verify().
✗ Incorrect
verify(service, times(2)).process("data") asserts the method was called exactly twice with argument "data". Option A verifies two separate calls but does not check call count properly. Option A allows two or more calls, not exactly two. Option A checks only one call with any string.