diff --git a/src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java b/src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java index ab3036f0b..ea1e1870c 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java @@ -36,6 +36,8 @@ public class MigrateJUnitTestCase extends Recipe { private static final AnnotationMatcher JUNIT_TEST_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.Test"); + private static final AnnotationMatcher JUNIT_AFTER_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.*After*"); + private static final AnnotationMatcher JUNIT_BEFORE_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.*Before*"); private static boolean isSupertypeTestCase(@Nullable JavaType.FullyQualified fullyQualified) { if (fullyQualified == null || fullyQualified.getSupertype() == null || "java.lang.Object".equals(fullyQualified.getFullyQualifiedName())) { @@ -123,9 +125,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex updateCursor(md); if (md.getSimpleName().startsWith("test") && md.getLeadingAnnotations().stream().noneMatch(JUNIT_TEST_ANNOTATION_MATCHER::matches)) { md = updateMethodDeclarationAnnotationAndModifier(md, "@Test", "org.junit.jupiter.api.Test", ctx); - } else if ("setUp".equals(md.getSimpleName())) { + } else if ("setUp".equals(md.getSimpleName()) && md.getLeadingAnnotations().stream().noneMatch(JUNIT_BEFORE_ANNOTATION_MATCHER::matches)) { md = updateMethodDeclarationAnnotationAndModifier(md, "@BeforeEach", "org.junit.jupiter.api.BeforeEach", ctx); - } else if ("tearDown".equals(md.getSimpleName())) { + } else if ("tearDown".equals(md.getSimpleName()) && md.getLeadingAnnotations().stream().noneMatch(JUNIT_AFTER_ANNOTATION_MATCHER::matches)) { md = updateMethodDeclarationAnnotationAndModifier(md, "@AfterEach", "org.junit.jupiter.api.AfterEach", ctx); } return md; diff --git a/src/test/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCaseTest.java b/src/test/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCaseTest.java index 80084d858..ecf9c1fa2 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCaseTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCaseTest.java @@ -42,25 +42,25 @@ void convertTestCase() { java( """ import junit.framework.TestCase; - + public class MathTest extends TestCase { protected long value1; protected long value2; - + @Override protected void setUp() { super.setUp(); value1 = 2; value2 = 3; } - + public void testAdd() { setName("primitive test"); long result = value1 + value2; assertEquals(5, result); fail("some Failure message"); } - + @Override protected void tearDown() { super.tearDown(); @@ -73,19 +73,19 @@ protected void tearDown() { import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.*; public class MathTest { protected long value1; protected long value2; - + @BeforeEach public void setUp() { value1 = 2; value2 = 3; } - + @Test public void testAdd() { //setName("primitive test"); @@ -93,7 +93,7 @@ public void testAdd() { assertEquals(5, result); fail("some Failure message"); } - + @AfterEach public void tearDown() { value1 = 0; @@ -116,7 +116,7 @@ void convertExtendedTestCase() { public abstract class CTest extends TestCase { @Override public void setUp() {} - + @Override public void tearDown() {} } @@ -125,11 +125,11 @@ public void tearDown() {} package com.abc; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; - + public abstract class CTest { @BeforeEach public void setUp() {} - + @AfterEach public void tearDown() {} } @@ -143,18 +143,18 @@ public void tearDown() {} public class MathTest extends CTest { protected long value1; protected long value2; - + @Override protected void setUp() { value1 = 2; value2 = 3; } - + public void testAdd() { long result = value1 + value2; assertEquals(5, result); } - + @Override protected void tearDown() { value1 = 0; @@ -168,25 +168,25 @@ protected void tearDown() { import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - + import static org.junit.jupiter.api.Assertions.assertEquals; public class MathTest extends CTest { protected long value1; protected long value2; - + @BeforeEach public void setUp() { value1 = 2; value2 = 3; } - + @Test public void testAdd() { long result = value1 + value2; assertEquals(5, result); } - + @AfterEach public void tearDown() { value1 = 0; @@ -205,9 +205,9 @@ void notTestCaseHasTestCaseAssertion() { java( """ import org.junit.Test; - + import static junit.framework.TestCase.assertTrue; - + class AaTest { @Test public void someTest() { @@ -220,9 +220,9 @@ private boolean isSameStuff(String stuff) { """, """ import org.junit.Test; - + import static org.junit.jupiter.api.Assertions.assertTrue; - + class AaTest { @Test public void someTest() { @@ -244,9 +244,9 @@ void notTestCaseHasAssertAssertion() { java( """ import org.junit.Test; - + import static junit.framework.Assert.assertTrue; - + class AaTest { @Test public void someTest() { @@ -259,9 +259,9 @@ private boolean isSameStuff(String stuff) { """, """ import org.junit.Test; - + import static org.junit.jupiter.api.Assertions.assertTrue; - + class AaTest { @Test public void someTest() { @@ -293,4 +293,48 @@ public void testSomeNumberStuff() { ) ); } + + @Test + void avoidDuplicateAnnotations(){ + rewriteRun( + spec -> spec.recipes( + new MigrateJUnitTestCase(), + new UpdateBeforeAfterAnnotations() + ), + //language=java + java( + """ + import junit.framework.TestCase; + import org.junit.After; + import org.junit.Before; + + public class MathTest extends TestCase { + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + } + """, + """ + import org.junit.jupiter.api.AfterEach; + import org.junit.jupiter.api.BeforeEach; + + public class MathTest { + + @BeforeEach + public void setUp() { + } + + @AfterEach + public void tearDown() { + } + } + """ + ) + ); + } }