Mocking static methods with Mockito
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Mockito is a popular mocking framework for unit tests in Java. Traditionally, mocking static methods has been complicated because Mockito did not support it directly. However, starting from Mockito 3.4.0, the addition of the mockito-inline artifact allows for mocking of static methods, opening new possibilities for unit testing code that uses static calls.
Understanding the Challenge with Static Methods
Static methods belong to the class rather than any instance of the class. This nature makes them have a global state which, in turn, makes testing environments prone to side effects if not handled appropriately. Prior to Mockito 3.4.0, developers relied on other tools like PowerMock, which extends Mockito capabilities to deal with such scenarios. The ability to mock static methods directly with Mockito simplifies test setups and reduces the leaning curve for new developers.
Setting Up Mockito for Static Method Mocking
To begin mocking static methods with Mockito, you will need to ensure you include the mockito-inline dependency in your project. Here is an example of how you might set up this dependency using Maven:
How to Mock Static Methods
To mock a static method with Mockito, you use the MockedStatic interface provided by Mockito. This interface functions similarly to the standard mocking mechanism but specifically targets static methods. Here’s how you do it:
- Start a mock session: Use
Mockito.mockStatic(ClassToMock.class)to start a static mock session. This returns aMockedStaticinstance which will be used to configure and verify the static calls. - Specify the behavior: Once the session is started, you can define the behavior of the static methods using typical Mockito syntax (
when(),thenReturn(), etc.). - Close the mock session: The mock session should be closed after each test to ensure there's no state carry-over between tests. This is typically handled within a try-with-resources block to ensure automatic closure.
Example
Suppose you have a utility class with a static method:
You can test a class that uses this static method using Mockito as shown below:
Key Considerations
While Mockito now supports static method mocking, it should be used judiciously. Static method mocking can signify poor design choices leading to tightly coupled and less maintainable code. Therefore, consider design patterns and principles (like Dependency Injection) that may help in writing more testable and modular code.
Summary Table
| Feature | Tool | Usage Scope | Limitations |
| Static Method Mocking | Mockito prior v3.4.0 | Not supported directly | Required third-party extensions |
| Static Method Mocking | Mockito v3.4.0+ | Supported with mockito-inline | Continued caution for design impact |
In conclusion, Mockito's ability to mock static methods can significantly simplify testing in Java where static methods are used. The practical application of this feature should, however, be managed with consideration for good coding practices and design principles.

