Mockito atMost

The atMost method in the Mockito framework is used to verify that a method on a mock object was called no more than a specified number of times. This is useful when you want to ensure that a method is not invoked more than a certain number of times during the execution of your code.

Table of Contents

  1. Introduction
  2. atMost Method Syntax
  3. Examples
    • Basic Usage
    • Verifying Multiple Mocks
  4. Real-World Use Case
  5. Conclusion

Introduction

Mockito is a popular library in Java for creating and managing mock objects. The atMost method, which belongs to the Mockito class, allows you to specify that a method should not be called more than a given number of times on a mock object. This can help ensure that your code does not overuse certain methods, which could indicate potential performance issues or logical errors.

atMost Method Syntax

Verifying At Most a Maximum Number of Invocations

import static org.mockito.Mockito.atMost;

static VerificationMode atMost(int maxNumberOfInvocations)

Specifies that a method should have been called no more than the given number of times.

Parameters:

  • maxNumberOfInvocations: The maximum number of times the method should have been called.

Returns:

  • A VerificationMode object that can be used to verify the method calls.

Examples

Basic Usage

Verify that a method was called no more than a specified number of times on a single mock object.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.atMost;
import org.junit.jupiter.api.Test;

public class BasicAtMostTest {
    @Test
    void testAtMost() {
        UserService mockUserService = mock(UserService.class);

        // Perform some interactions
        mockUserService.deleteUser("123");
        mockUserService.deleteUser("123");

        // Verify that deleteUser was called at most twice
        verify(mockUserService, atMost(2)).deleteUser("123");
    }
}

class UserService {
    public void deleteUser(String userId) {
        // Method implementation
    }
}

Verifying Multiple Mocks

Verify that methods were called no more than a specified number of times on multiple mock objects.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.atMost;
import org.junit.jupiter.api.Test;

public class MultipleAtMostTest {
    @Test
    void testAtMostMultipleMocks() {
        UserService mockUserService = mock(UserService.class);
        NotificationService mockNotificationService = mock(NotificationService.class);

        // Perform some interactions
        mockUserService.deleteUser("123");
        mockNotificationService.sendNotification("User 123 deleted");

        // Verify that deleteUser was called at most once
        verify(mockUserService, atMost(1)).deleteUser("123");

        // Verify that sendNotification was called at most twice
        verify(mockNotificationService, atMost(2)).sendNotification("User 123 deleted");
    }
}

class UserService {
    public void deleteUser(String userId) {
        // Method implementation
    }
}

interface NotificationService {
    void sendNotification(String message);
}

Real-World Use Case

Ensuring Limited Method Calls in a Service

In a real-world scenario, you might want to ensure that certain methods in your service are not called excessively. This can help in validating that your code is efficient and does not perform redundant operations.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.atMost;
import org.junit.jupiter.api.Test;

interface PaymentService {
    void processPayment(String orderId);
}

interface NotificationService {
    void sendNotification(String message);
}

class OrderService {
    private final PaymentService paymentService;
    private final NotificationService notificationService;

    public OrderService(PaymentService paymentService, NotificationService notificationService) {
        this.paymentService = paymentService;
        this.notificationService = notificationService;
    }

    public void placeOrder(String orderId) {
        paymentService.processPayment(orderId);
        notificationService.sendNotification("Order " + orderId + " placed");
    }

    public void processMultiplePayments(String orderId, int times) {
        for (int i = 0; i < times; i++) {
            paymentService.processPayment(orderId);
        }
    }
}

public class OrderServiceTest {
    @Test
    void testOrderServiceInteractionsAtMost() {
        PaymentService mockPaymentService = mock(PaymentService.class);
        NotificationService mockNotificationService = mock(NotificationService.class);
        OrderService orderService = new OrderService(mockPaymentService, mockNotificationService);

        // Call methods on OrderService
        orderService.placeOrder("123");
        orderService.processMultiplePayments("123", 2);

        // Verify that processPayment was called at most three times
        verify(mockPaymentService, atMost(3)).processPayment("123");

        // Verify that sendNotification was called at most once
        verify(mockNotificationService, atMost(1)).sendNotification("Order 123 placed");
    }
}

In this example, the OrderServiceTest class uses Mockito’s atMost method to ensure that processPayment is called no more than three times and sendNotification is called no more than once. This helps ensure that the OrderService methods do not perform excessive operations.

Conclusion

The atMost verification mode in Mockito is used for ensuring that methods on mock objects are not called more than a specified number of times. By using atMost, you can validate that your code does not overuse certain methods, improving the efficiency and reliability of your tests. This helps ensure that your code behaves as expected and performs optimally.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top