From cb2af2c1137722b9908cbd0351f5061be7b00401 Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Mon, 30 Sep 2024 18:15:13 +0200 Subject: [PATCH] `PowerMockitoMockStaticToMockito` should not remove existing `mockStatic` initializer (#612) * When mockito `mockStatic` is used as variable initializer we shouldn't return null * Apply suggestions from code review --------- Co-authored-by: Tim te Beek --- .../PowerMockitoMockStaticToMockito.java | 4 +++- .../PowerMockitoMockStaticToMockitoTest.java | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockito.java b/src/main/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockito.java index c1f7d2133..019425f50 100644 --- a/src/main/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockito.java +++ b/src/main/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockito.java @@ -190,7 +190,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) if (MOCKED_STATIC_MATCHER.matches(mi)) { determineTestGroups(); - if (!getCursor().getPath(o -> o instanceof J.Assignment || o instanceof J.Try.Resource).hasNext()) { + if (!getCursor().getPath(o -> o instanceof J.VariableDeclarations || + o instanceof J.Assignment || + o instanceof J.Try.Resource).hasNext()) { //noinspection DataFlowIssue return null; } diff --git a/src/test/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockitoTest.java index a9fa9a0cb..81625d90d 100644 --- a/src/test/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/mockito/PowerMockitoMockStaticToMockitoTest.java @@ -681,6 +681,25 @@ public void testStaticMethod() { ); } + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/611") + void existingMockitoMockStaticShouldNotBeTouched() { + //language=java + rewriteRun( + java( + """ + import static org.mockito.Mockito.mockStatic; + + import org.mockito.MockedStatic; + + class TestClass { + MockedStatic mocked = mockStatic(String.class); + } + """ + ) + ); + } + @Test @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/358") void doesNotExplodeOnTopLevelMethodDeclaration() {