From 8d07aec58a1c8b36eada6aa2dcaf7da3488f015a Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 13 Dec 2024 22:50:50 +0100 Subject: [PATCH] Use `doAfterVisit(ChangeMethodName)` to rename test methods Fixes #258 --- .../testing/cleanup/RemoveTestPrefix.java | 7 ++-- .../testing/cleanup/RemoveTestPrefixTest.java | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java b/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java index a5ac744b3..240bd0250 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java @@ -21,7 +21,9 @@ import org.openrewrite.TreeVisitor; import org.openrewrite.internal.NameCaseConvention; import org.openrewrite.java.AnnotationMatcher; +import org.openrewrite.java.ChangeMethodName; import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.J.MethodDeclaration; @@ -138,9 +140,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi } // Rename method and return - type = type.withName(newMethodName); - return m.withName(m.getName().withSimpleName(newMethodName).withType(type)) - .withMethodType(type); + doAfterVisit(new ChangeMethodName(MethodMatcher.methodPattern(m), newMethodName, false, false).getVisitor()); + return m; } private boolean methodExists(JavaType.Method method, String newName) { diff --git a/src/test/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefixTest.java b/src/test/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefixTest.java index aa7e8e238..944b49fad 100644 --- a/src/test/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefixTest.java +++ b/src/test/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefixTest.java @@ -377,4 +377,40 @@ void of() { ) ); } + + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/258") + @Test + void removeTestPrefixWhenCalled() { + rewriteRun( + // language=java + java( + """ + import org.junit.jupiter.api.Test; + + public class FooTest { + @Test + void bar() { + testFoo(); + } + + @Test + void testFoo() {} + } + """, + """ + import org.junit.jupiter.api.Test; + + public class FooTest { + @Test + void bar() { + foo(); + } + + @Test + void foo() {} + } + """ + ) + ); + } }