Mockito JUnit 5 Example

Introduction

Mockito is a powerful mocking framework for unit tests in Java. When combined with JUnit 5, it allows for effective and efficient testing of Java applications. This blog post provides an example of how to use Mockito with JUnit 5, covering the essential annotations and methods to create and manage mocks.

Maven Dependencies

To use Mockito with JUnit 5, add the following dependencies to your pom.xml file:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.9.2</version>
    <scope>test</scope>
</dependency>

Example Scenario

We will create a CalculatorService class that has a dependency on an AdditionService. Our goal is to test the CalculatorService methods using Mockito to mock the AdditionService.

CalculatorService and AdditionService Classes

public class AdditionService {
    public int add(int a, int b) {
        return a + b;
    }
}

public class CalculatorService {
    private final AdditionService additionService;

    public CalculatorService(AdditionService additionService) {
        this.additionService = additionService;
    }

    public int performAddition(int a, int b) {
        return additionService.add(a, b);
    }
}

JUnit 5 Test Class with Mockito

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

@ExtendWith(MockitoExtension.class)
public class CalculatorServiceTest {

    @Mock
    private AdditionService additionService;

    @InjectMocks
    private CalculatorService calculatorService;

    @Test
    public void testPerformAddition() {
        // Arrange
        when(additionService.add(10, 20)).thenReturn(30);

        // Act
        int result = calculatorService.performAddition(10, 20);

        // Assert
        assertEquals(30, result);
        verify(additionService).add(10, 20);
    }
}

Explanation

  1. Annotations:

    • @ExtendWith(MockitoExtension.class): Integrates Mockito with JUnit 5, enabling the use of Mockito annotations.
    • @Mock: Creates a mock instance of the AdditionService class.
    • @InjectMocks: Injects the mock AdditionService into the CalculatorService instance.
  2. Arrange:

    • when(additionService.add(10, 20)).thenReturn(30): Configures the mock AdditionService to return 30 when the add method is called with arguments 10 and 20.
  3. Act:

    • int result = calculatorService.performAddition(10, 20): Calls the performAddition method on the CalculatorService instance.
  4. Assert:

    • assertEquals(30, result): Asserts that the result of the performAddition method is 30.
    • verify(additionService).add(10, 20): Verifies that the add method was called with the expected arguments.

Summary

Using Mockito with JUnit 5 allows for easy and effective unit testing by mocking dependencies and verifying interactions. This example demonstrated how to create a mock of a dependency, inject it into the class being tested, and verify the behavior using Mockito and JUnit 5. By leveraging these tools, you can write comprehensive and maintainable unit tests for your Java applications.

Comments