0
0
JunitHow-ToBeginner ยท 3 min read

How to Mock Static Methods with Mockito in JUnit Tests

To mock a static method in JUnit using Mockito, use Mockito.mockStatic(Class.class) inside a try-with-resources block. Then stub the static method call with when() and verify or assert as usual. This requires Mockito 3.4.0+ and JUnit 5 or 4.
๐Ÿ“

Syntax

Use Mockito.mockStatic(Class.class) to create a mock scope for static methods. Inside a try-with-resources block, stub static methods with when() and define return values. After the block, the static mocking is automatically closed.

  • Class.class: The class containing the static method.
  • mockStatic(): Creates a mock context for static methods.
  • when(): Defines behavior for the static method call.
  • Try-with-resources ensures the static mock is closed properly.
java
try (MockedStatic<ClassName> mockedStatic = Mockito.mockStatic(ClassName.class)) {
    mockedStatic.when(() -> ClassName.staticMethod(args)).thenReturn(value);
    // test code using the mocked static method
}
๐Ÿ’ป

Example

This example shows how to mock a static method Utility.calculate() that returns an int. The test mocks the static method to return 10 instead of its real implementation.

java
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

class Utility {
    public static int calculate() {
        return 5; // original implementation
    }
}

public class StaticMockTest {

    @Test
    void testMockStaticMethod() {
        try (MockedStatic<Utility> mockedStatic = Mockito.mockStatic(Utility.class)) {
            mockedStatic.when(Utility::calculate).thenReturn(10);

            int result = Utility.calculate();

            assertEquals(10, result);
        }
    }
}
Output
Test passed
โš ๏ธ

Common Pitfalls

  • Not using try-with-resources can cause static mocks to leak and affect other tests.
  • Using Mockito versions older than 3.4.0 will not support static mocking.
  • Forgetting to import org.mockito.MockedStatic or Mockito.mockStatic().
  • Mocking static methods outside the scope of mockStatic() block will call the real method.
java
/* Wrong: static mock not closed properly */
MockedStatic<Utility> mockedStatic = Mockito.mockStatic(Utility.class);
mockedStatic.when(Utility::calculate).thenReturn(10);
int result = Utility.calculate(); // works
// forgot to close mockedStatic - can cause issues

/* Right: use try-with-resources */
try (MockedStatic<Utility> mockedStatic = Mockito.mockStatic(Utility.class)) {
    mockedStatic.when(Utility::calculate).thenReturn(10);
    int result = Utility.calculate(); // mocked
}
๐Ÿ“Š

Quick Reference

StepDescription
1Use Mockito.mockStatic(Class.class) inside try-with-resources
2Stub static method with mockedStatic.when(() -> Class.staticMethod()).thenReturn(value)
3Call the static method inside the try block to get mocked result
4Mockito automatically closes the static mock after the block
โœ…

Key Takeaways

Use Mockito.mockStatic(Class.class) with try-with-resources to mock static methods safely.
Mockito static mocking requires version 3.4.0 or higher.
Always stub static methods inside the mockStatic block to avoid calling real methods.
Close static mocks automatically by using try-with-resources to prevent test interference.
Verify or assert static method behavior inside the mock scope for reliable tests.