Configuring PowerMock to use Mockito's `mock-maker-inline` feature involves a few steps. This setup allows you to leverage the capabilities of both PowerMock and Mockito's inline mock maker, which is particularly useful for mocking final classes and methods.
Step 1: Understand the Basics
- PowerMock and Mockito Compatibility: Ensure that your versions of PowerMock and Mockito are compatible. PowerMock version 2.0.0 and above supports Mockito 2, but there might be some compatibility issues with certain features[1].
- Mockito Inline Mock Maker: This feature allows mocking of final classes and methods. It was introduced in Mockito 2.1.0 and requires a Java agent for instrumentation[4].
Step 2: Configure Mockito Inline Mock Maker
To enable Mockito's `mock-maker-inline`, you need to create a specific configuration file:
1. Create the Configuration File:
- Navigate to your project's `src/test/resources` directory.
- Create a directory named `mockito-extensions` if it doesn't exist.
- Inside `mockito-extensions`, create a file named `org.mockito.plugins.MockMaker`.
- Add the following line to this file: `mock-maker-inline`[2].
Step 3: Configure PowerMock
PowerMock can interfere with Mockito's `mock-maker-inline` due to its own `MockMaker`. To resolve this, you need to configure PowerMock to delegate to Mockito's `mock-maker-inline`:
1. Configure PowerMock's MockMaker:
- Create a file named `powermock-configuration.properties` in the `org/powermock/extensions` directory under your project's resources.
- Add the following line to this file: `mockito.mock-maker-class=mock-maker-inline`[1].
Step 4: Use PowerMock with Mockito
To use PowerMock with Mockito in your tests, ensure you have the correct annotations:
1. Annotations:
- Annotate your test class with `@RunWith(PowerMockRunner.class)`.
- Use `@PrepareForTest` to specify the classes that you want to mock static methods for[3].
Example
Here's a simple example of how you might use PowerMock with Mockito's inline mock maker:
java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.verify;
@RunWith(PowerMockRunner.class)
@PrepareForTest(YourClassToMock.class)
public class YourTest {
@Test
public void testYourMethod() {
// Mocking static methods or final classes using PowerMockito
PowerMockito.mockStatic(YourClassToMock.class);
// Your test logic here
// Verify interactions
verifyStatic(YourClassToMock.class);
}
}
Important Notes
- Classloader Issues: There might be classloader issues when using both PowerMock and Mockito's `mock-maker-inline` in the same project. Ensure that you handle these issues by configuring PowerMock correctly[6].
- Java Agent Dependency: Mockito's inline mock maker requires a Java agent for instrumentation. Ensure that you have the necessary dependencies in your project[8].
[1] https://github.com/powermock/powermock/wiki/mockito
[2] https://davidvlijmincx.com/posts/writing_higher_quality_tests_with_mockitos_inline_mock_maker/
[3] https://www.baeldung.com/intro-to-powermock
[4] https://stackoverflow.com/questions/14292863/how-to-mock-a-final-class-with-mockito
[5] https://stackoverflow.com/questions/47327981/integration-tests-with-powermock-and-spring-boot
[6] https://github.com/powermock/powermock/issues/1034
[7] https://github.com/powermock/powermock/wiki/Getting-Started
[8] https://stackoverflow.com/a/55667381