Test Overview
This test mocks a void method in a dependency to verify it is called correctly without executing its real behavior.
This test mocks a void method in a dependency to verify it is called correctly without executing its real behavior.
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; class Service { void performAction() { // Real implementation } } class Client { private final Service service; Client(Service service) { this.service = service; } void execute() { service.performAction(); } } public class ClientTest { @Test void testExecute_callsPerformAction() { Service mockService = mock(Service.class); Client client = new Client(mockService); client.execute(); verify(mockService).performAction(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized | — | PASS |
| 2 | Mockito creates a mock instance of Service | Service mock created with no real behavior | — | PASS |
| 3 | Client instance created with mocked Service | Client ready to use mockService | — | PASS |
| 4 | Client.execute() is called | Inside execute, service.performAction() is called on mock | — | PASS |
| 5 | Mockito verifies performAction() was called on mockService | Verification checks call history on mockService | performAction() was called exactly once | PASS |
| 6 | Test ends successfully | No exceptions thrown, all verifications passed | — | PASS |
sendEmail() to do nothing when called?doNothing().when(mock).method() to mock void methods doing nothing.when() or verify().doThrow(new RuntimeException()).when(mock).deleteFile(); mock.deleteFile();
mock.deleteFile() is called?mock.deleteFile() triggers the RuntimeException as mocked.doNothing().when(mock.deleteFile());
doNothing().when(mock).method(), not passing method call inside when().logAction() was called exactly twice during a test. Which Mockito code correctly does this?verify(mock, times(n)).method() to check method called n times.Pass the test and this pattern moves unknown