Test Overview
This test verifies how many times a mock method is called using JUnit with Mockito. It checks that a method is called exactly twice, never called with a certain argument, and called at least once.
This test verifies how many times a mock method is called using JUnit with Mockito. It checks that a method is called exactly twice, never called with a certain argument, and called at least once.
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; public class VerificationTimesTest { interface Service { void performAction(String input); } @Test void testVerificationTimes() { Service mockService = mock(Service.class); // Call performAction twice with "hello" mockService.performAction("hello"); mockService.performAction("hello"); // Call performAction once with "world" mockService.performAction("world"); // Verify performAction called exactly twice with "hello" verify(mockService, times(2)).performAction("hello"); // Verify performAction never called with "test" verify(mockService, never()).performAction("test"); // Verify performAction called at least once with "world" verify(mockService, atLeast(1)).performAction("world"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized, Mockito mock created for Service interface | — | PASS |
| 2 | Calls mockService.performAction("hello") twice | Mock records two calls with argument "hello" | — | PASS |
| 3 | Calls mockService.performAction("world") once | Mock records one call with argument "world" | — | PASS |
| 4 | Verifies performAction called exactly twice with "hello" using verify(mockService, times(2)) | Mockito checks recorded calls for "hello" argument | Exactly 2 calls with "hello" found | PASS |
| 5 | Verifies performAction never called with "test" using verify(mockService, never()) | Mockito checks recorded calls for "test" argument | No calls with "test" found | PASS |
| 6 | Verifies performAction called at least once with "world" using verify(mockService, atLeast(1)) | Mockito checks recorded calls for "world" argument | At least 1 call with "world" found | PASS |
| 7 | Test ends successfully | All verifications passed | — | PASS |
Mockito verification method checks that a method was called exactly 3 times?times(n) usagetimes(n) method verifies that the method was called exactly n times.times(3) is the correct choice.verify(mock, times(3)).method() -> Option Dnever() to verify that a method was not called at all.verify(mock, never()).method();. Using times(0) also works but never() is clearer and preferred.verify(mock, atLeast(2)).process();
atLeast(n) meaningatLeast(n) method verifies that the method was called at least n times, meaning 2 or more times.atLeast(2), it means the method must be called 2 or more times, not exactly 2.process() must be called at least 2 times or more. -> Option Cverify(mock, never).execute();
never() method requires parentheses to be called as a method.never without parentheses, which is a syntax error.update() was called at least once but no more than 5 times. Which Mockito verification code correctly expresses this?update() must be called at least once and at most 5 times.atLeast(1) to check minimum calls and atMost(5) to check maximum calls.Pass the test and this pattern moves unknown