Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any specific configurations required for PowerMock to work with Mockito's InlineMockMaker


Are there any specific configurations required for PowerMock to work with Mockito's InlineMockMaker


PowerMock and Mockito's InlineMockMaker have specific configuration requirements to work together effectively. Here's a detailed overview of how to configure them:

Understanding PowerMock and Mockito's InlineMockMaker

- PowerMock: This library extends Mockito by allowing the mocking of static methods, final classes, and methods. It is commonly used when Mockito alone cannot handle certain mocking scenarios[2][6].

- Mockito's InlineMockMaker: Introduced in Mockito 5.0.0, this feature allows for more efficient mocking of final classes and methods without needing additional libraries like PowerMock. However, it requires specific configuration to work with PowerMock[1][5].

Configuring PowerMock with Mockito's InlineMockMaker

**Incompatibility: PowerMock implements its own `MockMaker`, which can lead to incompatibility with Mockito's `mock-maker-inline`. If both are present, only one can be used, and it's undetermined which one will be chosen[10].

**Solution: To use Mockito's `mock-maker-inline` with PowerMock, you need to configure PowerMock to delegate calls to another `MockMaker`. This can be achieved by creating a configuration file.

1. Create a Configuration File:
- Create a file named `org/powermock/extensions/configuration.properties` in your classpath.
- Add the following line to this file:


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

This tells PowerMock to use Mockito's `mock-maker-inline` for mocking[10].

2. Using Mockito's InlineMockMaker Directly:
- If you're not using PowerMock for a specific test, you can enable Mockito's `mock-maker-inline` by adding a file named `org.mockito.plugins.MockMaker` in the `src/test/resources/mockito-extensions` directory with the content:


     mock-maker-inline
     

However, if PowerMock is also in the classpath, this might not work reliably due to the incompatibility mentioned[5][10].

3. Choosing Between PowerMock and Mockito's InlineMockMaker:
- For scenarios where you need to mock static methods or final classes and methods, PowerMock is still necessary.
- For projects that only require mocking of final classes and methods without static methods, using Mockito's `mock-maker-inline` alone might be sufficient and more straightforward[1][7].

Conclusion

While PowerMock and Mockito's InlineMockMaker can be used together with proper configuration, they serve different purposes. PowerMock remains essential for mocking static methods, while Mockito's InlineMockMaker offers a more streamlined approach for mocking final classes and methods without the need for additional libraries.

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://github.com/powermock/powermock/wiki/Getting-Started
[4] https://stackoverflow.com/questions/6036450/powermock-mockito-vs-mockito-alone
[5] https://davidvlijmincx.com/posts/writing_higher_quality_tests_with_mockitos_inline_mock_maker/
[6] https://www.baeldung.com/intro-to-powermock
[7] https://www.reddit.com/r/java/comments/2w2nqj/beginners_guide_to_using_mockito_and_powermockito/
[8] https://docs.openrewrite.org/recipes/java/testing/mockito/replacepowermockito
[9] https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.mockito.plugins.MockMaker
[10] https://github.com/powermock/powermock/wiki/mockito