Contents

Mock static methods with Mockito

Introduction

This post explains how to mock static methods. Mockito doesn't support this behavior by default. To stub static 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 static methods. We can add a dependency or create a file inside the test resources to enable the new Mock maker.

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 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 Static method

With MockedStatic, we can stub static methods of a class. The code uses try-with-resources to limit the scope of the stubbing to only within that try statement. The example below shows how to mock the static method Instant.now(). We first create a mock of Instant for the static method to return. Then we stub the static method and make it return the object we created earlier. Every time we call the stubbed static method we get our mocked object back.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@Test
void mockingStaticMethods() {
    try (MockedStatic<Instant> mocked = mockStatic(Instant.class)) {
        // Create a mock for the static method to return
        var mockInstant = mock(Instant.class);
        when(mockInstant.getEpochSecond()).thenReturn(1L);
    
        // Stub the static method .now()
        mocked.when(Instant::now).thenReturn(mockInstant);
    
        var result = Instant.now();
    
        assertThat(result.getEpochSecond()).isEqualTo(1);
    }
}

Mock static method and change the default behavior

This example looks the same as the stubbing example, but now we pass an extra parameter to MockedStatic. When we pass withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS) to the mockStatic method, we call the real method for the methods we didn't stub. By default, if we don't supply the extra parameter, it will behave like any other mockito mock and return null when we call methods we didn't stub. The example below shows how to stub a static method and pass other calls to the real methods.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
@Test
void mockingStaticMethods() {
    try (MockedStatic<LocalDateTime> mocked = mockStatic(LocalDateTime.class, withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS))) {
        // creating a mock for the stub to return
        var mockLocalDateTime = mock(LocalDateTime.class);
        when(mockLocalDateTime.getMinute()).thenReturn(30);
        
        // stub the static method
        mocked.when(LocalDateTime::now).thenReturn(mockLocalDateTime);
        
        var result = LocalDateTime.now();
        assertThat(result.getMinute()).isEqualTo(30);

        // calling a static method that we didn't stub
        LocalDateTime localDateTime = LocalDateTime.of(2022, 02, 22, 19, 35, 30);
        // this print localDateTime = 2022-02-22T19:35:30
        System.out.println("localDateTime = " + localDateTime);
    }
}

Conclusion

In this post, we enabled the inline mock maker to mock static methods. We also saw two examples of how to create a MockedStatic object and change its default behavior.

If you are 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:

If you want to read more about Java when I write about it – you can follow me on Twitter.