Mockito never method

The never method in the Mockito framework is used to verify that a method on a mock object was never called. It is an alias for times(0), providing a more readable and intuitive way to assert that no interactions occurred.

Table of Contents

  1. Introduction
  2. never 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 never method, which belongs to the Mockito class, allows you to specify that a method should not have been called on a mock object. This can help ensure that certain operations do not occur during the execution of your code.

never Method Syntax

Verifying No Invocations

import static org.mockito.Mockito.never;

static VerificationMode never()

Specifies that a method should not have been called.

Returns:

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

Examples

Basic Usage

Verify that a method was never called on a single mock object.

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

public class BasicNeverTest {
    @Test
    void testNever() {
        UserService mockUserService = mock(UserService.class);

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

        // Verify that updateUser was never called
        verify(mockUserService, never()).updateUser("123");
    }
}

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

    public void updateUser(String userId) {
        // Method implementation
    }
}

Verifying Multiple Mocks

Verify that methods were never called on multiple mock objects.

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

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

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

        // Verify that updateUser was never called
        verify(mockUserService, never()).updateUser("123");

        // Verify that sendNotification was never called
        verify(mockNotificationService, never()).sendNotification("User 123 deleted");
    }
}

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

    public void updateUser(String userId) {
        // Method implementation
    }
}

interface NotificationService {
    void sendNotification(String message);
}

Real-World Use Case

Ensuring No Unintended Calls

In a real-world scenario, you might want to ensure that certain methods in your service are not called under specific conditions. This can help in validating that your code does not perform unintended operations.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;
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 cancelOrder(String orderId) {
        // Perform order cancellation logic
        notificationService.sendNotification("Order " + orderId + " canceled");
    }
}

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

        // Call the cancelOrder method
        orderService.cancelOrder("123");

        // Verify that processPayment was never called
        verify(mockPaymentService, never()).processPayment("123");

        // Verify that sendNotification was called
        verify(mockNotificationService).sendNotification("Order 123 canceled");
    }
}

In this example, the OrderServiceTest class uses Mockito’s never method to ensure that processPayment is never called when an order is canceled, but that sendNotification is called. This helps ensure that the OrderService methods perform the correct operations and avoid unintended ones.

Conclusion

The never verification mode in Mockito is used for ensuring that methods on mock objects are not called. By using never, you can validate that your code does not perform unintended operations, improving the accuracy and reliability of your tests. This helps ensure that your code behaves as expected and avoids unnecessary actions.

Leave a Comment

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

Scroll to Top