Skip to content

Commit 9b80dde

Browse files
committed
Added support to update Test.class in UpdateTestAnnotation.
fixes #284
1 parent b5fe015 commit 9b80dde

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/main/java/org/openrewrite/java/testing/junit5/UpdateTestAnnotation.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.time.Duration;
3131
import java.util.Comparator;
32+
import java.util.Set;
3233

3334
public class UpdateTestAnnotation extends Recipe {
3435

@@ -61,6 +62,13 @@ private static class UpdateTestAnnotationVisitor extends JavaIsoVisitor<Executio
6162
@Override
6263
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
6364
J.CompilationUnit c = super.visitCompilationUnit(cu, executionContext);
65+
Set<NameTree> nameTreeSet = c.findType("org.junit.Test");
66+
if (!nameTreeSet.isEmpty()) {
67+
// Update other references like `Test.class`.
68+
c = (J.CompilationUnit) new ChangeType("org.junit.Test", "org.junit.jupiter.api.Test", true)
69+
.getVisitor().visit(cu, executionContext);
70+
}
71+
6472
maybeRemoveImport("org.junit.Test");
6573
doAfterVisit(new JavaIsoVisitor<ExecutionContext>() {
6674
@Override

src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ public void defaults(RecipeSpec spec) {
3737
.activateRecipes("org.openrewrite.java.testing.junit5.JUnit4to5Migration"));
3838
}
3939

40+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/284")
41+
@Test
42+
void classReference() {
43+
rewriteRun(
44+
java(
45+
"""
46+
import org.junit.Test;
47+
48+
public class Sample {
49+
void method() {
50+
Class<Test> c = Test.class;
51+
}
52+
}
53+
""",
54+
"""
55+
import org.junit.jupiter.api.Test;
56+
57+
public class Sample {
58+
void method() {
59+
Class<Test> c = Test.class;
60+
}
61+
}
62+
"""
63+
)
64+
);
65+
}
66+
4067
@Test
4168
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/145")
4269
void assertThatReceiver() {

0 commit comments

Comments
 (0)