Skip to content

Commit 7796693

Browse files
authored
Remove empty constructors and suite() methods in MigrateJUnitTestCase (#926)
After removing `super(testName)`, constructors left with an empty body are now fully removed. Static `suite()` methods returning `junit.framework.Test` or `TestSuite` are also removed along with their stale imports. Fixes moderneinc/customer-requests#1948
1 parent 5c07d95 commit 7796693

2 files changed

Lines changed: 85 additions & 1 deletion

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,28 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
119119
}
120120

121121
@Override
122-
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
122+
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
123123
J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx);
124124
updateCursor(md);
125+
126+
// After super.visitMethodDeclaration (which triggers visitMethodInvocation
127+
// to remove super(testName)), check if this is now an empty constructor
128+
if (md.isConstructor() &&
129+
(md.getBody() == null || md.getBody().getStatements().isEmpty())) {
130+
return null;
131+
}
132+
133+
// Remove suite() methods that return junit.framework.Test or TestSuite
134+
if ("suite".equals(md.getSimpleName()) &&
135+
md.hasModifier(J.Modifier.Type.Static) &&
136+
md.getMethodType() != null &&
137+
(TypeUtils.isOfClassType(md.getMethodType().getReturnType(), "junit.framework.Test") ||
138+
TypeUtils.isOfClassType(md.getMethodType().getReturnType(), "junit.framework.TestSuite"))) {
139+
maybeRemoveImport("junit.framework.Test");
140+
maybeRemoveImport("junit.framework.TestSuite");
141+
return null;
142+
}
143+
125144
if (md.getSimpleName().startsWith("test") && md.getLeadingAnnotations().stream().noneMatch(JUNIT_TEST_ANNOTATION_MATCHER::matches)) {
126145
md = updateMethodDeclarationAnnotationAndModifier(md, "@Test", "org.junit.jupiter.api.Test", ctx);
127146
} else if ("setUp".equals(md.getSimpleName()) && md.getLeadingAnnotations().stream().noneMatch(JUNIT_BEFORE_ANNOTATION_MATCHER::matches)) {

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,72 @@ public AppTest(String testName) {
354354
""",
355355
"""
356356
public class AppTest {
357+
}
358+
"""
359+
)
360+
);
361+
}
362+
363+
@Test
364+
void constructorWithAdditionalStatementsIsKept() {
365+
//language=java
366+
rewriteRun(
367+
java(
368+
"""
369+
import junit.framework.TestCase;
370+
371+
public class AppTest extends TestCase {
372+
private final String name;
357373
public AppTest(String testName) {
374+
super(testName);
375+
this.name = testName;
376+
}
377+
}
378+
""",
379+
"""
380+
public class AppTest {
381+
private final String name;
382+
public AppTest(String testName) {
383+
this.name = testName;
384+
}
385+
}
386+
"""
387+
)
388+
);
389+
}
390+
391+
@Test
392+
void suiteMethodIsRemoved() {
393+
//language=java
394+
rewriteRun(
395+
java(
396+
"""
397+
import junit.framework.Test;
398+
import junit.framework.TestCase;
399+
import junit.framework.TestSuite;
400+
401+
public class AppTest extends TestCase {
402+
public AppTest(String testName) {
403+
super(testName);
404+
}
405+
public static Test suite() {
406+
return new TestSuite(AppTest.class);
407+
}
408+
public void testApp() {
409+
assertTrue(true);
410+
}
411+
}
412+
""",
413+
"""
414+
import org.junit.jupiter.api.Test;
415+
416+
import static org.junit.jupiter.api.Assertions.assertTrue;
417+
418+
public class AppTest {
419+
420+
@Test
421+
public void testApp() {
422+
assertTrue(true);
358423
}
359424
}
360425
"""

0 commit comments

Comments
 (0)