0
0
JUnittesting~7 mins

Mocking static methods (Mockito 3.4+) in JUnit

Choose your learning style9 modes available
Introduction

Sometimes, you need to test code that calls static methods. Mocking static methods lets you replace their behavior during tests to control results and avoid side effects.

When your code calls utility methods that are static and you want to simulate different return values.
When static methods access external resources like databases or files and you want to avoid those during tests.
When you want to isolate the unit under test from static dependencies to test it independently.
When you want to verify that a static method was called with specific arguments.
When refactoring legacy code that uses static methods and you want to add tests without changing the code.
Syntax
JUnit
try (MockedStatic<ClassName> mockedStatic = Mockito.mockStatic(ClassName.class)) {
    mockedStatic.when(() -> ClassName.staticMethod(args)).thenReturn(mockedValue);
    // test code that calls ClassName.staticMethod
}

Use try-with-resources to automatically close the static mock after the test.

Inside the try block, you define what the static method should return or do.

Examples
This mocks Math.max(1, 2) to return 10 instead of 2.
JUnit
try (MockedStatic<Math> mathMock = Mockito.mockStatic(Math.class)) {
    mathMock.when(() -> Math.max(1, 2)).thenReturn(10);
    int result = Math.max(1, 2);
    assertEquals(10, result);
}
This mocks a static method getConfig() to return a custom string.
JUnit
try (MockedStatic<Utils> utilsMock = Mockito.mockStatic(Utils.class)) {
    utilsMock.when(() -> Utils.getConfig()).thenReturn("mockedConfig");
    String config = Utils.getConfig();
    assertEquals("mockedConfig", config);
}
Sample Program

This test mocks the static method Calculator.add(2, 3) to return 100 instead of 5. The assertion checks that the mocked value is returned.

JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

class StaticMethodTest {

    static class Calculator {
        static int add(int a, int b) {
            return a + b;
        }
    }

    @Test
    void testMockStaticAdd() {
        try (MockedStatic<Calculator> mocked = Mockito.mockStatic(Calculator.class)) {
            mocked.when(() -> Calculator.add(2, 3)).thenReturn(100);
            int result = Calculator.add(2, 3);
            assertEquals(100, result);
        }
    }
}
OutputSuccess
Important Notes

Mockito static mocking requires Mockito 3.4 or higher and JUnit 5 for best integration.

Always use try-with-resources to avoid static mocks leaking between tests.

Mocking static methods can make tests harder to read; use it only when necessary.

Summary

Mocking static methods lets you control static method behavior during tests.

Use try-with-resources and Mockito.mockStatic to create static mocks.

This helps isolate code and test units that depend on static methods.