Mockito BDDMockito verify Method

The verify method in the BDDMockito class is part of the Behavior-Driven Development (BDD) style of writing tests using Mockito. It is used to verify that specific interactions happened on mock objects. This method is particularly useful for making tests more readable and aligning with the BDD style, which focuses on describing the behavior of the application in a clear and human-readable format.

Table of Contents

  1. Introduction
  2. verify Method Syntax
  3. Examples
    • Basic Usage
    • Using verify with Verification Modes
    • Using verify with Argument Matchers
  4. Real-World Use Case
  5. Conclusion

Introduction

Mockito’s BDDMockito class provides methods that support the BDD (Behavior-Driven Development ) style of writing tests. The verify method is used to ensure that specific interactions happened on mock objects, making tests more readable and intuitive.

verify Method Syntax

Verifying Interactions

import org.mockito.BDDMockito;

static <T> T verify(T mock)

Verifies that specific interactions happened on the given mock object.

Parameters:

  • mock: The mock object whose interactions are being verified.

Returns:

  • The mock object itself for further verification.

Verifying Interactions with Verification Mode

import org.mockito.BDDMockito;
import org.mockito.verification.VerificationMode;

static <T> T verify(T mock, VerificationMode mode)

Verifies that specific interactions happened on the given mock object with a specified verification mode.

Parameters:

  • mock: The mock object whose interactions are being verified.
  • mode: The verification mode that specifies the number of times the interaction should have happened.

Returns:

  • The mock object itself for further verification.

Examples

Basic Usage

Use verify to ensure that a method was called on a mock object.

import static org.mockito.BDDMockito.*;
import org.junit.jupiter.api.Test;

public class BasicVerifyTest {
    @Test
    void testVerify() {
        UserService mockUserService = mock(UserService.class);

        // Call the method
        mockUserService.getUserDetails("user123");

        // Verify the interaction
        verify(mockUserService).getUserDetails("user123");
    }
}

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

Using verify with Verification Modes

Use verify with verification modes to check the number of times a method was called on a mock object.

import static org.mockito.BDDMockito.*;
import org.junit.jupiter.api.Test;

public class VerifyWithVerificationModesTest {
    @Test
    void testVerifyWithVerificationModes() {
        UserService mockUserService = mock(UserService.class);

        // Call the method multiple times
        mockUserService.getUserDetails("user123");
        mockUserService.getUserDetails("user123");

        // Verify the interaction with times
        verify(mockUserService, times(2)).getUserDetails("user123");
    }
}

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

Using verify with Argument Matchers

Use verify with argument matchers to ensure that a method was called with specific arguments on a mock object.

import static org.mockito.BDDMockito.*;
import static org.mockito.ArgumentMatchers.*;
import org.junit.jupiter.api.Test;

public class VerifyWithArgumentMatchersTest {
    @Test
    void testVerifyWithArgumentMatchers() {
        NotificationService mockNotificationService = mock(NotificationService.class);

        // Call the method
        mockNotificationService.sendNotification("user123", "Notification message");

        // Verify the interaction with argument matchers
        verify(mockNotificationService).sendNotification(eq("user123"), anyString());
    }
}

interface NotificationService {
    void sendNotification(String recipient, String message);
}

Real-World Use Case

Verifying Interactions in Services

In a real-world scenario, you might need to verify that specific methods were called on a service’s dependencies to ensure that certain operations were performed as expected. Using verify can simplify these tests by allowing you to specify the expected interactions, making your tests more readable and intuitive.

import static org.mockito.BDDMockito.*;
import org.junit.jupiter.api.Test;

interface EmailService {
    void sendEmail(String recipient, String subject, String body);
}

class UserService {
    private final EmailService emailService;

    public UserService(EmailService emailService) {
        this.emailService = emailService;
    }

    public void notifyUser(String userId, String message) {
        emailService.sendEmail(userId, "Notification", message);
    }
}

public class UserServiceTest {
    @Test
    void testNotifyUser() {
        EmailService mockEmailService = mock(EmailService.class);
        UserService userService = new UserService(mockEmailService);

        // Call the method
        userService.notifyUser("user123", "Your account has been updated.");

        // Verify the interaction
        verify(mockEmailService).sendEmail("user123", "Notification", "Your account has been updated.");
    }
}

In this example, the UserServiceTest class uses Mockito’s BDDMockito.verify method to ensure that the sendEmail method was called on the EmailService mock. This simplifies the test by allowing you to verify the interaction in a readable and intuitive way.

Output

Mockito BDDMockito.verify Method

Conclusion

The BDDMockito.verify method in Mockito is used for verifying interactions with mock objects in a BDD style. By using verify, you can make your tests more readable and align them with the BDD approach, focusing on the behavior of the application rather than the implementation details. This helps ensure that your tests are clear, comprehensive, and easy to understand.

Leave a Comment

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

Scroll to Top