Mockito ignoreStubs Method

The ignoreStubs method in Mockito is used to ignore stubbed methods of given mocks for the sake of verification. This allows you to focus on verifying only the interactions that were not explicitly stubbed, which can simplify your test verifications by ignoring expected behaviors that are not part of the test focus.

Table of Contents

  1. Introduction
  2. ignoreStubs 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 ignoreStubs method allows you to ignore interactions with stubbed methods when verifying interactions on mock objects. This is useful when you have mocks with many stubs and you want to verify only the interactions that were not predefined.

ignoreStubs Method Syntax

Ignoring Stubbed Methods

static Object[] ignoreStubs(Object... mocks)

Ignores stubbed methods of the given mocks for verification purposes.

Parameters:

  • mocks: The mock objects whose stubbed methods should be ignored during verification.

Returns:

  • An array of the given mock objects.

Examples

Basic Usage

Ignore stubbed methods for a single mock object.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.ignoreStubs;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import org.junit.jupiter.api.Test;

public class BasicIgnoreStubsTest {
    @Test
    void testIgnoreStubs() {
        UserService mockUserService = mock(UserService.class);

        // Stub a method
        when(mockUserService.getUserDetails("123")).thenReturn("Mock user details");

        // Call the stubbed method
        mockUserService.getUserDetails("123");

        // Call another method that is not stubbed
        mockUserService.deleteUser("123");

        // Ignore stubbed methods for verification
        ignoreStubs(mockUserService);

        // Verify the unstubbed method call
        verify(mockUserService).deleteUser("123");

        // Verify no more interactions except the ignored stubs
        verifyNoMoreInteractions(mockUserService);
    }
}

class UserService {
    public String getUserDetails(String userId) {
        return "Real user details for " + userId;
    }

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

Verifying Multiple Mocks

Ignore stubbed methods for multiple mock objects.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.ignoreStubs;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import org.junit.jupiter.api.Test;

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

        // Stub methods
        when(mockUserService.getUserDetails("123")).thenReturn("Mock user details");
        when(mockNotificationService.sendNotification("User 123 deleted")).thenReturn(true);

        // Call the stubbed methods
        mockUserService.getUserDetails("123");
        mockNotificationService.sendNotification("User 123 deleted");

        // Call another method that is not stubbed
        mockUserService.deleteUser("123");

        // Ignore stubbed methods for verification
        ignoreStubs(mockUserService, mockNotificationService);

        // Verify the unstubbed method call
        verify(mockUserService).deleteUser("123");

        // Verify no more interactions except the ignored stubs
        verifyNoMoreInteractions(mockUserService, mockNotificationService);
    }
}

class UserService {
    public String getUserDetails(String userId) {
        return "Real user details for " + userId;
    }

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

interface NotificationService {
    boolean sendNotification(String message);
}

Real-World Use Case

Simplifying Verification in Complex Tests

In a real-world scenario, you might have complex tests with multiple mocks and numerous stubbed methods. Using ignoreStubs can simplify your verifications by allowing you to focus on the interactions that are part of the test logic, ignoring those that are predefined and expected.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.ignoreStubs;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verifyNoMoreInteractions;
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 cancelOrder(String orderId) {
        notificationService.sendNotification("Order " + orderId + " canceled");
    }
}

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

        // Stub methods
        when(mockNotificationService.sendNotification("Order 123 placed")).thenReturn(true);

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

        // Ignore stubbed methods for verification
        ignoreStubs(mockNotificationService);

        // Verify the unstubbed method calls
        verify(mockNotificationService).sendNotification("Order 123 canceled");

        // Verify no more interactions except the ignored stubs
        verifyNoMoreInteractions(mockPaymentService, mockNotificationService);
    }
}

In this example, the OrderServiceTest class uses Mockito’s ignoreStubs method to ignore the stubbed sendNotification method when verifying interactions. This allows the test to focus on verifying the actual test logic, simplifying the verification process.

Conclusion

The ignoreStubs method in Mockito is used for ignoring stubbed methods during verification. By using ignoreStubs, you can focus on verifying only the interactions that are part of your test logic, making your tests clearer and more focused. This helps ensure that your tests are comprehensive and accurately reflect the behavior of your code.

Leave a Comment

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

Scroll to Top