0
0
JUnittesting~15 mins

Verification times (times, never, atLeast) in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify method call counts using Mockito verification times
Preconditions (3)
Step 1: Create a mock of Calculator
Step 2: Inject the mock into CalculatorService
Step 3: Call CalculatorService.performAddition(2, 3) three times
Step 4: Verify that Calculator.add() was called exactly 3 times with arguments 2 and 3
Step 5: Verify that Calculator.add() was never called with arguments 5 and 5
Step 6: Verify that Calculator.add() was called at least 2 times with arguments 2 and 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)
Automation Requirements - JUnit 5 with Mockito
Assertions Needed:
Verify method add() called exactly 3 times with specific arguments
Verify method add() never called with other arguments
Verify method add() called at least 2 times with specific arguments
Best Practices:
Use Mockito.mock() to create mocks
Use Mockito.verify() with times(), never(), atLeast() for verification
Use descriptive method and variable names
Keep test methods focused and clear
Automated Solution
JUnit
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:

  • times(3): Ensures add(2, 3) was called exactly three times.
  • never(): Ensures add(5, 5) was never called.
  • atLeast(2): Ensures add(2, 3) was called at least two times.

This test confirms the method call counts precisely as required.

Common Mistakes - 3 Pitfalls
Using verify() without specifying times() or never()
Verifying method calls with wrong arguments
Calling verify() before the method under test is executed
Bonus Challenge

Now add data-driven testing to verify add() method call counts with three different input pairs: (1,1), (2,3), and (4,5)

Show Hint