Verify method call counts using Mockito verification times
Preconditions (3)
✅ Expected Result: All verifications pass: add() called exactly 3 times with (2,3), never called with (5,5), and at least 2 times with (2,3)
import static org.mockito.Mockito.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class Calculator { public int add(int a, int b) { return a + b; } } class CalculatorService { private final Calculator calculator; public CalculatorService(Calculator calculator) { this.calculator = calculator; } public int performAddition(int a, int b) { return calculator.add(a, b); } } public class CalculatorServiceTest { private Calculator calculatorMock; private CalculatorService calculatorService; @BeforeEach void setup() { calculatorMock = mock(Calculator.class); calculatorService = new CalculatorService(calculatorMock); } @Test void testAddMethodCallVerification() { // Call performAddition three times with (2,3) calculatorService.performAddition(2, 3); calculatorService.performAddition(2, 3); calculatorService.performAddition(2, 3); // Verify add called exactly 3 times with (2,3) verify(calculatorMock, times(3)).add(2, 3); // Verify add never called with (5,5) verify(calculatorMock, never()).add(5, 5); // Verify add called at least 2 times with (2,3) verify(calculatorMock, atLeast(2)).add(2, 3); } }
The code starts by creating a mock of the Calculator class using mock(Calculator.class). This mock is injected into the CalculatorService.
We call performAddition(2, 3) three times, which internally calls the mocked add method.
Then, we verify three things using Mockito's verify method with different times:
add(2, 3) was called exactly three times.add(5, 5) was never called.add(2, 3) was called at least two times.This test confirms the method call counts precisely as required.
Now add data-driven testing to verify add() method call counts with three different input pairs: (1,1), (2,3), and (4,5)
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