A project I am working on involves updating our codebase to JUnit 5. A number of our test classes had previously been using PowerMockito for static mocking. As PowerMockito does not currently support JUnit 5, we updated our Mockito dependency and switched to using Mockito's static mocking. This works for the most part when running the unit tests but has issues when the tests are run with pitest to get mutation coverage.
Despite the tests running and passing fine with mvn test
or mvn verify
, pitest will give the error:
[ERROR] Failed to execute goal org.pitest:pitest-maven:1.5.2:mutationCoverage (default-cli) on project <PROJECT>: Execution default-cli of goal org.pitest:pitest-maven:1.5.2:mutationCoverage failed: 9 tests did not pass without mutation when calculating line coverage. Mutation testing requires a green suite.
The 9 tests mentioned are the only tests that use static mocking with Mockito.
The tests generally look like this:
Sample Static Mocking Test
@ExtendWith(MockitoExtension.class)
public class SampleTest {
@Test
public void sampleTestWithMocking() {
String param = "test";
String expected = "value";
MockedStatic<MyClass> mockStaticMyClass = Mockito.mockStatic(MyClass.class);
mockStaticMyClass.when(() -> MyClass.myStaticMethod(param)).thenReturn(expected);
assertEquals(expected, MyClass.myStaticMethod(param));
}
}
Pitest does not currently support static mocking with mockito. I'll see if it could be supported, but it likely to be a complex task. Support for Powermock required dark magics (rewriting the bytecode of the bytcode manipulation library it uses), and was always brittle and easily broken by new Powermock releases.
A better long term solution would be to remove the need for static mocking from the test suite. Although it does have some use cases, it is most often a red flag for design issues.