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 |