Mockito JUnit 5 Example

1. Overview

Mockito is a widely used testing framework that allows developers to create and use mock objects in unit tests. 

JUnit 5 is the next generation of JUnit, which brings many powerful features and improvements. 

Integrating Mockito with JUnit 5 enables efficient testing, helping to ensure our applications run as expected. In this tutorial, we will walk through how to set up a Mockito project with JUnit 5 and demonstrate its fundamental capabilities.

2. Development Steps

1. Set up a new Maven project.

2. Add Mockito and JUnit 5 dependencies.

3. Create a Calculator class which we will test.

4. Implement a test class using JUnit 5 and Mockito to test Calculator methods.

5. Run the tests and analyze the results.

3. Dependencies (Mockito and JUnit 5)

<!-- JUnit 5 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>
<!-- Mockito Core -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>5.6.0</version>
<scope>test</scope> </dependency>

4. Code Program

// The Calculator class we will be testing
class Calculator {
    public double add(double a, double b) {
        return a + b;
    }
    public double subtract(double a, double b) {
        return a - b;
    }
}

// Test class for Calculator using Mockito and JUnit 5
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
public class CalculatorTest {

    private Calculator calculator;
    
    @BeforeEach
    public void setUp() {
        calculator = mock(Calculator.class);
        when(calculator.add(1.0, 1.0)).thenReturn(2.0);
        when(calculator.subtract(2.0, 1.0)).thenReturn(1.0);
    }
    
    @Test
    public void testAddition() {
        assertEquals(2.0, calculator.add(1.0, 1.0));
        verify(calculator).add(1.0, 1.0);
    }
    
    @Test
    public void testSubtraction() {
        assertEquals(1.0, calculator.subtract(2.0, 1.0));
        verify(calculator).subtract(2.0, 1.0);
    }
}

Output:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

Code Explanation:

1. Our tutorial begins with a simple Calculator class that has add and subtract methods.

2. We then have a CalculatorTest class where we utilize both Mockito and JUnit 5 features.

3. @BeforeEach denotes that the setUp() method should run before each test. In this method, we create a mock instance of Calculator and define some behaviors using the when().thenReturn() pattern.

4. The testAddition() method tests the add method of Calculator. We first assert that the result of adding 1.0 and 1.0 using our mocked object is 2.0. After the assertion, we verify that our mock's add method was called with the expected arguments.

5. Similarly, testSubtraction() tests the subtract method.

6. The tests, when run, show that both methods pass, confirming that our mocked methods are working as expected.

5. Conclusion

Mockito integrated with JUnit 5 provides a powerful combination for unit testing in Java. With Mockito's ability to mock objects and JUnit's robust testing framework, we can ensure our application is tested thoroughly. This tutorial provides a foundation, and there's a lot more to explore in both Mockito and JUnit 5 to enhance our testing capabilities.

Comments