Mockito getMock Method

The getMock method in the Mockito framework is used to retrieve the mock object that was used for a particular stub. This can be useful when you need to access the mock object after setting up stubs or interactions, especially in more complex testing scenarios.

Table of Contents

  1. Introduction
  2. getMock Method Syntax
  3. Examples
    • Basic Usage
    • Verifying Interactions After Retrieving the Mock
  4. Real-World Use Case
  5. Conclusion

Introduction

Mockito is a popular library in Java for creating and managing mock objects. The getMock method allows you to retrieve the mock object that was used for a particular stub. This is useful in situations where you need to access the mock object after stubbing its methods or verifying its interactions.

getMock Method Syntax

Retrieving the Mock Object Used for a Stub

<M> M getMock()

Returns the mock object that was used for the stub.

Returns:

  • The mock object of type M that was used for the stub.

Examples

Basic Usage

Retrieve the mock object used for a stub and verify its interactions.

import static org.mockito.Mockito.*;
import org.mockito.stubbing.Stubber;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class BasicGetMockTest {
    @Test
    void testGetMock() {
        UserService mockUserService = mock(UserService.class);

        // Stubbing a method
        Stubber stubber = doNothing().when(mockUserService).deleteUser("123");

        // Retrieving the mock object
        UserService retrievedMock = stubber.getMock();

        // Verifying the interaction
        retrievedMock.deleteUser("123");
        verify(mockUserService).deleteUser("123");
    }
}

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

Verifying Interactions After Retrieving the Mock

Retrieve the mock object used for a stub and verify multiple interactions.

import static org.mockito.Mockito.*;
import org.mockito.stubbing.Stubber;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class VerifyInteractionsTest {
    @Test
    void testVerifyInteractions() {
        UserService mockUserService = mock(UserService.class);

        // Stubbing a method
        Stubber stubber = doNothing().when(mockUserService).deleteUser("123");

        // Retrieving the mock object
        UserService retrievedMock = stubber.getMock();

        // Performing interactions
        retrievedMock.deleteUser("123");
        retrievedMock.updateUser("123");

        // Verifying interactions
        verify(mockUserService).deleteUser("123");
        verify(mockUserService).updateUser("123");
    }
}

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

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

Real-World Use Case

Accessing Mocks in Complex Test Scenarios

In a real-world scenario, you might need to access mock objects in complex test scenarios where multiple stubs and interactions are involved. The getMock method allows you to retrieve the mock object and verify its interactions, ensuring that your test setup is correct.

import static org.mockito.Mockito.*;
import org.mockito.stubbing.Stubber;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

interface PaymentService {
    void processPayment(String orderId);
    void cancelPayment(String orderId);
}

class OrderService {
    private final PaymentService paymentService;

    public OrderService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    public void placeOrder(String orderId) {
        paymentService.processPayment(orderId);
    }

    public void cancelOrder(String orderId) {
        paymentService.cancelPayment(orderId);
    }
}

public class OrderServiceTest {
    @Test
    void testOrderServiceInteractions() {
        PaymentService mockPaymentService = mock(PaymentService.class);

        // Stubbing methods
        Stubber stubber = doNothing().when(mockPaymentService).processPayment("123");
        stubber.doNothing().when(mockPaymentService).cancelPayment("123");

        // Retrieving the mock object
        PaymentService retrievedMock = stubber.getMock();

        OrderService orderService = new OrderService(retrievedMock);

        // Performing interactions
        orderService.placeOrder("123");
        orderService.cancelOrder("123");

        // Verifying interactions
        verify(mockPaymentService).processPayment("123");
        verify(mockPaymentService).cancelPayment("123");
    }
}

In this example, the OrderServiceTest class uses Mockito’s getMock method to retrieve the mock object after stubbing methods. This allows the test to perform interactions with the OrderService and verify that the PaymentService methods are called as expected.

Conclusion

The getMock method in Mockito is used for retrieving the mock object that was used for a particular stub. By using getMock, you can access the mock object after setting up stubs or interactions, providing flexibility and control in your tests. This helps ensure that your tests are accurate and comprehensive, allowing you to validate both real and mocked behavior.

Leave a Comment

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

Scroll to Top