unit-testingmockitopowermockjunit5

Junit5 mock a static method


I want to mock a static method in JUnit 5. But unfortunately, JUnit 5 doesn’t support Mockito. Is there another method to achieve the same other than reverting back to JUnit 4?


Solution

  • The short answer is no, as the Mockito team is done with their work and is waiting for the JUnit team for an extension and are discussing here a lot.

    With some overhead you can: As JUnit 5 provides support for running legacy JUnit 4, and there you can use Mockito. So you can create tests in Junit4 for these cases:

    A sample project for migration setup with gradle and with mvn. From there I am using PowerMock 2.0 beta with Mockito 2.