The `mockStatic()` method in Mockito is used to mock static methods, which was introduced in Mockito version 3.4.0. This feature allows developers to create unit tests that involve static methods without needing additional libraries like PowerMock. Here's a detailed explanation of how it works:
Overview of `mockStatic()`
- Purpose: The `mockStatic()` method is designed to create a mock for the static methods of a class. It returns an object of type `MockedStatic`, which represents the active and scoped mock of a type's static methods.
- Scope: The mock is active only within the scope of the `try-with-resources` statement where it is created. Once the scope is exited, the mock is automatically released, and the original behavior of the static methods is restored[4][6].
Using `mockStatic()`
To use `mockStatic()`, follow these steps:
1. Import Necessary Classes: Ensure you import `org.mockito.MockedStatic` and use the `mockito-inline` dependency instead of `mockito-core` in your project[8].
2. Create a Mock: Use the `mockStatic()` method within a `try-with-resources` block to create a mock for the static methods of a class. This ensures the mock is properly closed after use.
java
try (MockedStatic mockStatic = Mockito.mockStatic(YourStaticClass.class)) {
// Configure the mock behavior here
}
3. Configure Mock Behavior: Use the `when()` method on the `MockedStatic` object to define the behavior of the static methods. You can specify return values or exceptions for specific method calls.
java
mockStatic.when(() -> YourStaticClass.staticMethod()).thenReturn("Mocked Value");
4. Verify Invocations: You can verify how many times a static method was invoked using the `verify()` method on the `MockedStatic` object.
java
mockStatic.verify(() -> YourStaticClass.staticMethod());
Example
Here's a complete example of using `mockStatic()` to mock a static method:
java
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
public class StaticMethodTest {
@Test
public void testStaticMethod() {
try (MockedStatic mockStatic = Mockito.mockStatic(UtilityClass.class)) {
// Mock the static method to return a specific value
mockStatic.when(() -> UtilityClass.getGreeting("John")).thenReturn("Mocked Hello");
// Call the static method and verify the result
String result = UtilityClass.getGreeting("John");
assert result.equals("Mocked Hello");
// Verify that the static method was called
mockStatic.verify(() -> UtilityClass.getGreeting("John"));
}
}
}
In this example, `UtilityClass.getGreeting("John")` is mocked to return `"Mocked Hello"`, and then the invocation is verified.
Advantages
- Easy to Use: The `mockStatic()` method simplifies the process of mocking static methods without requiring additional libraries.
- Scoped Mocking: The mock is automatically released after the test, ensuring that the original behavior is restored, which helps prevent interference between tests[4][6].
[1] https://www.devzery.com/post/mockito-mock-static-methods-a-comprehensive-approach
[2] https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.mockito.MockedStatic
[3] https://groups.google.com/g/powermock/c/4KOwR1GKr0o
[4] https://howtodoinjava.com/mockito/mock-static-methods/
[5] https://github.com/powermock/powermock/wiki/Mockito/cebcc5324cd3991cacdf5e1319209e5c4cd80fd3
[6] https://www.codementor.io/@noelkamphoa/how-to-mock-static-methods-with-mockito-2f2svdbu8x
[7] https://stackoverflow.com/questions/4482315/why-doesnt-mockito-mock-static-methods
[8] https://www.testim.io/blog/mocking-static-methods-mockito/