Mockito's static mock() method handles cleanup primarily through the use of a try-with-resources block or by manually closing the MockedStatic object to ensure the mocks are scoped and resources properly released. When you mock static methods in Mockito using the mockStatic(Class) method, it returns an instance of MockedStatic, which is an AutoCloseable resource. This design allows the static mocking to be temporary and limited to a particular block of code, avoiding interference in other tests or after the test finishes.
The recommended and typical pattern is to use the static mock inside a try-with-resources block. This way, the MockedStatic object is automatically closed once the block is exited, and the static method mock is cleaned up. Closing the MockedStatic object restores the class's static methods to their original behavior and frees internal resources, preventing issues such as memory leaks or test contamination. For example:
java
try (MockedStatic mockedStatic = Mockito.mockStatic(MyClass.class)) {
mockedStatic.when(MyClass::staticMethod).thenReturn("mocked value");
// test code here
}
// Once out of the try block, the static mocking is closed and cleaned up
If one chooses not to use the try-with-resources pattern or needs to control mock scope differently, then it is vital to manually close the MockedStatic instance by calling its `close()` method. This call is how Mockito explicitly cleans up the static mock state. Failing to call `close()` can lead to memory leaks or unexpected behavior in subsequent tests because the static mocking remains active beyond its intended lifespan.
The `close()` method removes the mocking framework's instrumentation from the static methods, restoring the static method's original implementation. This ensures that the changes made by the mock do not persist beyond the lifetime of the MockedStatic and that test isolation is preserved.
Mockito's internal mechanisms track static mocks and their active scopes to manage cleanup effectively. Once closed, the mock state is deregistered, and the mocking listeners or handlers are removed from the static method calls.
For frameworks like Spock, the best practice is to place the call to `mockStatic.close()` within cleanup blocks to guarantee cleanup after each test iteration, especially when static mocking is reused in multiple tests.
In summary, the cleanup of Mockito's static mock() method is handled by the controlled lifecycle of the MockedStatic object, either automatically via try-with-resources or manually by calling `close()`. This approach avoids side effects between tests, memory leaks, and maintains the integrity of the static methods by restoring their original behavior post-mock. It emphasizes resource management and test isolation by encouraging developers always to close static mocks after use. Mockito 5 and later versions have refined these capabilities, eliminating the need for external agents like mockito-inline.
Additionally, when mocking static void methods, Mockito mocks them to do nothing by default within the mocked scope. This default void mock behavior is also cleaned up upon the MockedStatic's closing.
In practice, this strategy ensures efficient and safe usage of static mocks in unit tests, aligning with Mockito's goal of minimizing test fragility and making static method mocking manageable and reliable.
References:- Always close the mock using `mockStatic.close()` in a cleanup block to avoid memory leaks or interference between tests.
- Using try-with-resources ensures static mocks remain temporary and scoped.
- Manually closing the mock is necessary if not using try-with-resources to prevent persistent mocking side effects.
- Mockito's MockedStatic is AutoCloseable, and proper closing restores the static method's original behavior.
- Best practices encourage cleanups in test frameworks to maintain test independence.