0
0
JUnittesting~20 mins

verify() for interaction verification in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mockito Interaction 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 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();
    }
}
ATest passes successfully
BTest fails with WantedButNotInvoked exception
CTest fails with TooManyActualInvocations exception
DTest fails with NullPointerException
Attempts:
2 left
💡 Hint
Mockito's verify() checks if the method was called exactly once by default.
assertion
intermediate
2: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() here
Averify(service, times(0)).process();
BverifyNoMoreInteractions(service);
Cverify(service).process();
Dverify(service, never()).process();
Attempts:
2 left
💡 Hint
Check the Mockito documentation for verifying zero invocations.
🔧 Debug
advanced
2: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();
ABecause verify() requires specifying times(2) explicitly
BBecause process() was never called
CBecause process() was called twice but verify() expects exactly one call by default
DBecause the mock was not initialized properly
Attempts:
2 left
💡 Hint
Check how many times process() was called versus what verify() expects.
🧠 Conceptual
advanced
2:00remaining
What is the purpose of verifyNoMoreInteractions() in Mockito?
Select the best description of what verifyNoMoreInteractions(mock) does in Mockito tests.
AIt resets the mock to remove all previous interactions
BIt checks that no other methods besides those already verified were called on the mock
CIt verifies that the mock was never called at all
DIt verifies that all methods on the mock were called at least once
Attempts:
2 left
💡 Hint
Think about verifying that no unexpected calls happened.
framework
expert
3: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");
Averify(service, times(2)).process("data");
Bverify(service).process("data"); verify(service).process("data");
Cverify(service, atLeast(2)).process("data");
Dverify(service, times(1)).process(anyString());
Attempts:
2 left
💡 Hint
Check how to specify exact call counts with arguments in verify().