Bird
Raised Fist0
JUnittesting~15 mins

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

Choose your learning style10 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

Practice

(1/5)
1. Which Mockito verification method checks that a method was called exactly 3 times?
easy
A. verify(mock, atMost(3)).method()
B. verify(mock, never()).method()
C. verify(mock, atLeast(3)).method()
D. verify(mock, times(3)).method()

Solution

  1. Step 1: Understand times(n) usage

    The times(n) method verifies that the method was called exactly n times.
  2. Step 2: Match the exact call count

    Since the question asks for exactly 3 calls, times(3) is the correct choice.
  3. Final Answer:

    verify(mock, times(3)).method() -> Option D
  4. Quick Check:

    Exact call count = times(n) [OK]
Hint: Use times(n) for exact call count verification [OK]
Common Mistakes:
  • Confusing times(n) with atLeast(n)
  • Using never() when calls exist
  • Using atMost(n) instead of times(n)
2. Which of the following is the correct syntax to verify that a method was never called using Mockito?
easy
A. verify(mock, never()).method();
B. verify(mock, atLeast(0)).method();
C. verify(mock, times(0));
D. verify(mock).never().method();

Solution

  1. Step 1: Identify the correct method for zero calls

    Mockito provides never() to verify that a method was not called at all.
  2. Step 2: Check syntax correctness

    The correct syntax is verify(mock, never()).method();. Using times(0) also works but never() is clearer and preferred.
  3. Final Answer:

    verify(mock, never()).method(); -> Option A
  4. Quick Check:

    No calls = never() [OK]
Hint: Use never() to verify zero calls clearly [OK]
Common Mistakes:
  • Using times(0) instead of never() for clarity
  • Incorrect chaining like verify(mock).never()
  • Using atLeast(0) which always passes
3. Given the following code snippet:
verify(mock, atLeast(2)).process();

Which statement is true about the verification?
medium
A. The method process() must be called exactly 2 times.
B. The method process() must be called at most 2 times.
C. The method process() must be called at least 2 times or more.
D. The method process() must never be called.

Solution

  1. Step 1: Understand atLeast(n) meaning

    The atLeast(n) method verifies that the method was called at least n times, meaning 2 or more times.
  2. Step 2: Match the verification condition

    Since the code uses atLeast(2), it means the method must be called 2 or more times, not exactly 2.
  3. Final Answer:

    The method process() must be called at least 2 times or more. -> Option C
  4. Quick Check:

    Minimum calls = atLeast(n) [OK]
Hint: atLeast(n) means minimum n calls, not exact [OK]
Common Mistakes:
  • Confusing atLeast(n) with times(n)
  • Thinking atLeast(n) means at most n
  • Assuming never() means zero calls only
4. Identify the error in this Mockito verification:
verify(mock, never).execute();
medium
A. Using times() instead of never()
B. Missing parentheses after never()
C. Incorrect method name execute()
D. No error, syntax is correct

Solution

  1. Step 1: Check syntax of never()

    The never() method requires parentheses to be called as a method.
  2. Step 2: Identify missing parentheses

    The code uses never without parentheses, which is a syntax error.
  3. Final Answer:

    Missing parentheses after never() -> Option B
  4. Quick Check:

    never() requires parentheses [OK]
Hint: Always add () after never to avoid syntax errors [OK]
Common Mistakes:
  • Omitting parentheses after never
  • Confusing never with times(0)
  • Assuming method name causes error
5. You want to verify that a method update() was called at least once but no more than 5 times. Which Mockito verification code correctly expresses this?
hard
A. verify(mock, atLeast(1)).update(); verify(mock, atMost(5)).update();
B. verify(mock, times(1)).update(); verify(mock, times(5)).update();
C. verify(mock, never()).update();
D. verify(mock, atLeast(5)).update();

Solution

  1. Step 1: Understand the requirement

    The method update() must be called at least once and at most 5 times.
  2. Step 2: Use correct verification methods

    Use atLeast(1) to check minimum calls and atMost(5) to check maximum calls.
  3. Step 3: Combine verifications

    Both verifications must be done separately to ensure the call count is between 1 and 5.
  4. Final Answer:

    verify(mock, atLeast(1)).update(); verify(mock, atMost(5)).update(); -> Option A
  5. Quick Check:

    Range check = atLeast + atMost [OK]
Hint: Combine atLeast(n) and atMost(m) for call count range [OK]
Common Mistakes:
  • Using times() twice with different values
  • Using never() when calls exist
  • Using only atLeast(5) which ignores max limit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard