Contents

Mock enum with Mockito

Writen by: David Vlijmincx

Introduction

This post explains how to mock enum methods and classes. Mockito doesn't support this behaviour by default. To stub an enum and its methods, we need to enable the inline mock maker first.

Maven Dependency

For the examples, I used version 4.2.0 of Mockito.

1
2
3
4
5
6
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>4.2.0</version>
    <scope>test</scope>
</dependency>

We also need the inline mock maker to stub the enum and its methods. We can add a dependency or create a file inside the test resources to enable the new Mock maker. Adding the following dependency is easier.

Option 1: Inline mock maker dependency

Add this dependency in your pom.

1
2
3
4
5
6
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>4.2.0</version>
    <scope>test</scope>
</dependency>

Option 2: Adding a file to enable the inline mock maker

Create a resources directory in your test directory if you do not have one already. Inside the resources create the directory mockito-extensions and in that directory the file org.mockito.plugins.MockMaker. The complete path with the file should look like this: src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker.

In the file org.mockito.plugins.MockMaker paste this text mock-maker-inline, now, the mock maker is available during tests.

Mocking and Enum and its methods

With the inline mock maker, mocking an enum or its method is the same as mocking any other class or method. Below we have an example that mocks the enum class Vehicle. At the bottom of the example, you can find the Enum we are mocking. It is an Enum with three vehicles that each have a different speed.

At the top, you will find the test method that mocks the enum. As you can see, it doesn't differ much from other test methods. We mock the getSpeed() method to return 900. After creating the mock and stubbing the getSpeed method, we use a simple class to show how to use the mock instance and check if the result is correct.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MockingEnumClass {

    @Test
    void test(){
        Vehicle mock = mock(Vehicle.class);
        when(mock.getSpeed()).thenReturn(900);

        TestSubject testSubject = new TestSubject(mock);

        Assertions.assertEquals(900, testSubject.getSpeed());
    }
}

class TestSubject{

    private Vehicle vehicle;

    public TestSubject(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    public int getSpeed(){
        return vehicle.getSpeed();
    }
}

enum Vehicle {
    BIKE(10),
    CAR(100),
    TRUCK(150);

    private int speed;

    Vehicle(int speed) {
        this.speed = speed;
    }

    int getSpeed(){
        return speed;
    }

}

Conclusion

In this post, we enabled the inline mock maker to mock an enum and its methods. We also saw an example of how to mock an enum and that it looks the same as mocking any other class or method.

If you're curious and want to know more about what you can do with Mockito, please check out their documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html. It lists all the Mockito features and how to use them.

Further reading

More about testing in Java: