Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the steps to configure PowerMock to use Mockito's mock-maker-inline


What are the steps to configure PowerMock to use Mockito's mock-maker-inline


Configuring PowerMock to work with Mockito's `mock-maker-inline` involves several steps due to their inherent incompatibilities. PowerMock implements its own `MockMaker`, which can lead to conflicts with Mockito's `mock-maker-inline`. However, you can configure PowerMock to delegate calls to Mockito's `mock-maker-inline` for tests that don't use PowerMock. Here's a detailed guide on how to set this up:

Step 1: Understand the Incompatibility

PowerMock and Mockito's `mock-maker-inline` are incompatible because both define a `MockMaker`. If both are present in the classpath, only one can be used, and which one is used is unpredictable[4].

Step 2: Configure PowerMock to Delegate to Mockito

To use Mockito's `mock-maker-inline` with PowerMock, you need to configure PowerMock to delegate to Mockito's `MockMaker`. This can be done by creating a configuration file for PowerMock.

1. Create a Configuration File: In your project's `src/test/resources` directory, create a folder named `org/powermock/extensions/` if it doesn't exist.
2. Add Configuration Properties: Inside the `org/powermock/extensions/` folder, create a file named `configuration.properties`.
3. Specify Mockito's MockMaker: In the `configuration.properties` file, add the following line to specify Mockito's `mock-maker-inline`:


   mockito.mock-maker-class=mock-maker-inline
   

This tells PowerMock to use Mockito's `mock-maker-inline` when it's not actively being used.

Step 3: Ensure Correct Dependencies

Make sure you have the correct dependencies in your project. You need both Mockito and PowerMock dependencies. For Maven, your `pom.xml` should include something like this:

xml

    
    
        org.mockito
        mockito-inline
        your-mockito-version
        test
    
    
    
    
        org.powermock
        powermock-api-mockito2
        your-powermock-version
        test
    
    
        org.powermock
        powermock-module-junit4
        your-powermock-version
        test
    

Replace `your-mockito-version` and `your-powermock-version` with the versions you are using.

Step 4: Use PowerMock and Mockito Appropriately

- For Tests Using PowerMock: Use `@RunWith(PowerMockRunner.class)` and `@PrepareForTest` annotations to enable PowerMock's functionality for mocking static methods or other PowerMock features.
- For Tests Not Using PowerMock: These tests can use Mockito's `mock-maker-inline` without issues, as PowerMock will delegate to Mockito's `MockMaker`.

Conclusion

By following these steps, you can configure your project to use both PowerMock for its extended features (like mocking static methods) and Mockito's `mock-maker-inline` for its benefits (such as mocking final classes and methods) in different tests. However, keep in mind that PowerMock does not support JUnit 5 yet, so you might need to stick with JUnit 4 for tests involving PowerMock[2][4].
Citations:
[1] https://rieckpil.de/mock-java-constructors-and-their-object-creation-with-mockito/
[2] https://www.digitalocean.com/community/tutorials/mockito-mock-static-method-powermock
[3] https://intellij-support.jetbrains.com/hc/en-us/community/posts/10002813476754-IntelliJ-IDEA-2022-2-4-errors-when-running-Mockito-tests
[4] https://github.com/powermock/powermock/wiki/mockito
[5] https://www.baeldung.com/intro-to-powermock
[6] https://softwareengineering.stackexchange.com/questions/188299/is-this-an-appropriate-use-of-mockitos-reset-method
[7] https://davidvlijmincx.com/posts/writing_higher_quality_tests_with_mockitos_inline_mock_maker/
[8] https://stackoverflow.com/questions/47327981/integration-tests-with-powermock-and-spring-boot