Mockito verifyNoInteractions Method

The verifyNoInteractions method in Mockito is used to verify that no interactions have occurred on the specified mock objects. This is useful for ensuring that certain methods are not called during the execution of your tests, which can help validate that your code behaves as expected.

Table of Contents

  1. Introduction
  2. verifyNoInteractions 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 verifyNoInteractions method allows you to check that no methods have been called on the specified mocks. This can be useful for verifying that certain parts of your code are not interacting with dependencies inappropriately or unexpectedly.

verifyNoInteractions Method Syntax

Verifying No Interactions on Mocks

static void verifyNoInteractions(Object... mocks)

Verifies that no interactions have occurred on the given mock objects.

Parameters:

  • mocks: The mock objects to verify for no interactions.

Returns:

  • Nothing. If any interactions have occurred, an org.mockito.exceptions.verification.NoInteractionsWanted exception is thrown.

Examples

Basic Usage

Verify that no interactions have occurred on a single mock object.

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

public class BasicVerifyNoInteractionsTest {
    @Test
    void testVerifyNoInteractions() {
        UserService mockUserService = mock(UserService.class);
        
        // Perform some actions that do not involve mockUserService

        // Verify that no interactions have occurred on the mockUserService
        verifyNoInteractions(mockUserService);
    }
}

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

Verifying Multiple Mocks

Verify that no interactions have occurred on multiple mock objects.

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

public class MultipleVerifyNoInteractionsTest {
    @Test
    void testVerifyNoInteractionsMultipleMocks() {
        UserService mockUserService = mock(UserService.class);
        NotificationService mockNotificationService = mock(NotificationService.class);
        
        // Perform some actions that do not involve mockUserService and mockNotificationService

        // Verify that no interactions have occurred on the mockUserService and mockNotificationService
        verifyNoInteractions(mockUserService, mockNotificationService);
    }
}

class UserService {
    public void deleteUser(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 services or methods are not called during the execution of your tests. This can help verify that your code is not making unintended calls to dependencies.

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

interface NotificationService {
    void sendNotification(String message);
}

class UserService {
    private final NotificationService notificationService;

    public UserService(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    public void deleteUser(String userId, boolean notify) {
        // Perform user deletion logic
        if (notify) {
            notificationService.sendNotification("User " + userId + " deleted");
        }
    }
}

public class UserServiceTest {
    @Test
    void testDeleteUserWithoutNotification() {
        NotificationService mockNotificationService = mock(NotificationService.class);
        UserService userService = new UserService(mockNotificationService);
        
        // Call the deleteUser method without notification
        userService.deleteUser("123", false);

        // Verify that no interactions have occurred on the mockNotificationService
        verifyNoInteractions(mockNotificationService);
    }

    @Test
    void testDeleteUserWithNotification() {
        NotificationService mockNotificationService = mock(NotificationService.class);
        UserService userService = new UserService(mockNotificationService);
        
        // Call the deleteUser method with notification
        userService.deleteUser("123", true);

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

In this example, the UserServiceTest class uses Mockito’s verifyNoInteractions method to ensure that the sendNotification method is not called when the notify flag is set to false. The test also verifies that the notification is sent when the notify flag is set to true.

Conclusion

The verifyNoInteractions method in Mockito is used for ensuring that no interactions have occurred on specified mock objects. By using verifyNoInteractions, you can validate that certain parts of your code do not interact with dependencies unexpectedly. This helps improve the reliability and accuracy of your tests.

Leave a Comment

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

Scroll to Top