PowerMockitoMockStaticToMockito removes mockStatic initialization
What version of OpenRewrite are you using?
I am using
- Maven plugin v5.0.0
- rewrite-testing-frameworks v2.0.1
How are you running OpenRewrite?
I am using the Maven plugin, from the command line: mvn org.openrewrite.maven:rewrite-maven-plugin:5.0.0:run -Drewrite.activeRecipes=org.openrewrite.java.testing.mockito.PowerMockitoMockStaticToMockito -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-java-security:LATEST,org.openrewrite.recipe:rewrite-kubernetes:LATEST,org.openrewrite.recipe:rewrite-logging-frameworks:LATEST,org.openrewrite.recipe:rewrite-migrate-java:LATEST,org.openrewrite.recipe:rewrite-spring:LATEST,org.openrewrite.recipe:rewrite-testing-frameworks:LATEST -Drewrite.exclusions=**api**.yaml
What is the smallest, simplest way to reproduce the problem?
class A {
@Test
void testTryWithResource() throws IOException {
try (MockedStatic<Files> mocked = mockStatic(Files.class)) {
// test logic that uses mocked
}
}
}
What did you expect to see?
class A {
@Test
void testTryWithResource() throws IOException {
try (MockedStatic<Files> mocked = mockStatic(Files.class)) {
// test logic that uses mocked
}
}
}
What did you see instead?
class A {
@Test
void testTryWithResource() throws IOException {
try (MockedStatic<Files> mocked) {
// test logic that uses mocked, which now provides a warning: `Variable 'mocked' might not have been initialized`
}
}
}
Note that I didn't include the import in the examples, it remains intact
What is the full stack trace of any errors you encountered?
None while the recipe ran
Are you interested in contributing a fix to OpenRewrite?
I'm also encountering this. It seems that this rewrite does not check if the used mockStatic import originates from PowerMock instead of Mockito Core
For now I've made it such that the PowerMockito migration also upgrades to Mockito 5 (https://github.com/openrewrite/rewrite-testing-frameworks/commit/7999511a3cf4e61c54b4aba2b674416cc24ddbc1), instead of the other way around that we had before. If we are to reinstate the PowerMockito migration when upgrading Mockito itself, then we should remove that circular recipe dependency.