Skip to content

Commit

Permalink
Use doAfterVisit(ChangeMethodName) to rename test methods
Browse files Browse the repository at this point in the history
Fixes #258
  • Loading branch information
timtebeek committed Dec 13, 2024
1 parent b10165d commit 8d07aec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
"""
)
);
}
}

0 comments on commit 8d07aec

Please sign in to comment.