Mockito atMostOnce

The atMostOnce method in the Mockito framework is used to verify that a method on a mock object was called no more than once. This is useful when you want to ensure that a method is invoked at most one time during the execution of your code, which can help catch unintended multiple invocations.

Table of Contents

  1. Introduction
  2. atMostOnce 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 atMostOnce method, which belongs to the Mockito class, allows you to specify that a method should not be called more than once on a mock object. This can help ensure that your code does not perform redundant operations and behaves as expected.

atMostOnce Method Syntax

Verifying At Most One Invocation

import static org.mockito.Mockito.atMostOnce;

static VerificationMode atMostOnce()

Specifies that a method should have been called no more than once.

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 once on a single mock object.

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

public class BasicAtMostOnceTest {
    @Test
    void testAtMostOnce() {
        UserService mockUserService = mock(UserService.class);

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

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

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

Verifying Multiple Mocks

Verify that methods were called no more than once on multiple mock objects.

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

public class MultipleAtMostOnceTest {
    @Test
    void testAtMostOnceMultipleMocks() {
        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, atMostOnce()).deleteUser("123");

        // Verify that sendNotification was called at most once
        verify(mockNotificationService, atMostOnce()).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 called no more than once. This can help in validating that your code does not perform redundant operations.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.atMostOnce;
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 class OrderServiceTest {
    @Test
    void testOrderServiceInteractionsAtMostOnce() {
        PaymentService mockPaymentService = mock(PaymentService.class);
        NotificationService mockNotificationService = mock(NotificationService.class);
        OrderService orderService = new OrderService(mockPaymentService, mockNotificationService);

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

        // Verify that processPayment was called at most once
        verify(mockPaymentService, atMostOnce()).processPayment("123");

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

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

Conclusion

The atMostOnce verification mode in Mockito is used for ensuring that methods on mock objects are not called more than once. By using atMostOnce, you can validate that your code does not perform redundant operations, 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