Mockito atLeastOnce

The atLeastOnce method in Mockito is used to verify that a method on a mock object was called at least once. This is useful when you want to ensure that a method has been invoked at least one time, regardless of how many times it was actually called.

Table of Contents

  1. Introduction
  2. atLeastOnce 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 atLeastOnce verification mode allows you to verify that a method was called at least once on a mock object. This can help ensure that important methods are invoked as expected during the execution of your code.

atLeastOnce Method Syntax

Verifying At Least One Invocation

import static org.mockito.Mockito.atLeastOnce;

static VerificationMode atLeastOnce()

Specifies that a method should have been called at least once.

Returns:

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

Examples

Basic Usage

Verify that a method was called at least once on a single mock object.

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

public class BasicAtLeastOnceTest {
    @Test
    void testAtLeastOnce() {
        UserService mockUserService = mock(UserService.class);

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

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

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

Verifying Multiple Mocks

Verify that methods were called at least once on multiple mock objects.

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

public class MultipleAtLeastOnceTest {
    @Test
    void testAtLeastOnceMultipleMocks() {
        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 least once
        verify(mockUserService, atLeastOnce()).deleteUser("123");

        // Verify that sendNotification was called at least once
        verify(mockNotificationService, atLeastOnce()).sendNotification("User 123 deleted");
    }
}

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

interface NotificationService {
    void sendNotification(String message);
}

Real-World Use Case

Ensuring Minimum Method Calls in a Service

In a real-world scenario, you might want to ensure that certain critical methods in your service are called at least once. This can be crucial for validating that essential operations are performed as expected.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.atLeastOnce;
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 testOrderServiceInteractionsAtLeastOnce() {
        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 least once
        verify(mockPaymentService, atLeastOnce()).processPayment("123");

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

In this example, the OrderServiceTest class uses Mockito’s atLeastOnce method to ensure that processPayment and sendNotification are called at least once. This helps ensure that critical operations in the OrderService are performed as expected.

Conclusion

The atLeastOnce verification mode in Mockito is used for ensuring that methods on mock objects are called at least once. By using atLeastOnce, you can validate that important methods are invoked the required number of times, improving the accuracy and reliability of your tests. This helps ensure that your code behaves as expected and that essential operations are performed.

Leave a Comment

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

Scroll to Top