Mockito Mock vs Stub

Introduction

In Mockito, mocks and stubs are used to simulate the behavior of real objects in unit tests. While both are used to isolate the code under test, they serve different purposes and have distinct characteristics. This article will explain the differences between mocks and stubs in Mockito and provide a summary table for quick reference.

Mocks

A mock is a test double that simulates the behavior of a real object. Mocks are used to verify interactions between the tested object and its dependencies. In Mockito, mocks are created using the @Mock annotation or the mock() method.

Example

import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.List;

@ExtendWith(MockitoExtension.class)
public class MockExampleTest {

    @Mock
    private List<String> mockList;

    @Test
    public void testMock() {
        mockList.add("test");
        verify(mockList).add("test");
    }
}

Key Points:

  • Mocks are used to verify interactions.
  • Mocks can throw exceptions or return predefined values.

Stubs

A stub is a test double that provides predetermined responses to method calls. Stubs are primarily used to set up the environment for the test by providing specific return values. In Mockito, stubs are created using the when() method.

Example

import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.extension.ExtendWith;

interface Service {
    String getData();
}

@ExtendWith(MockitoExtension.class)
public class StubExampleTest {

    @Test
    public void testStub() {
        Service stubService = mock(Service.class);
        when(stubService.getData()).thenReturn("stubbed data");

        String result = stubService.getData();

        assertEquals("stubbed data", result);
    }
}

Key Points:

  • Stubs provide predefined responses.
  • Stubs are primarily used to set up test data.

Differences Between Mock and Stub

Feature Mock Stub
Purpose Verify interactions Provide predefined responses
Creation @Mock annotation or mock() method mock() method + when() method
Behavior Simulates behavior, can throw exceptions Returns specific values, no exceptions
Verification Yes, verifies method calls No, used only to return values
Focus Interaction testing State-based testing
Typical Use Case Ensuring correct method calls Setting up test environment

Conclusion

Mocks and stubs are essential concepts in unit testing with Mockito. Understanding their differences and appropriate use cases helps you write more effective and maintainable tests. Mocks focus on interaction testing, verifying that methods are called with the expected arguments. Stubs, on the other hand, are used to set up the test environment by providing specific return values. By leveraging both mocks and stubs, you can thoroughly test your code's behavior and interactions.

Comments