diff --git a/src/main/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotations.java b/src/main/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotations.java index 832634e32..1809e14ab 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotations.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotations.java @@ -49,15 +49,13 @@ public TreeVisitor getVisitor() { public static class UpdateBeforeAfterAnnotationsVisitor extends JavaIsoVisitor { @Override - public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { - //This visitor handles changing the method visibility for any method annotated with one of the four before/after - //annotations. It registers visitors that will sweep behind it making the type changes. + public J preVisit(J tree, ExecutionContext ctx) { + stopAfterPreVisit(); doAfterVisit(new ChangeType("org.junit.Before", "org.junit.jupiter.api.BeforeEach", true).getVisitor()); doAfterVisit(new ChangeType("org.junit.After", "org.junit.jupiter.api.AfterEach", true).getVisitor()); doAfterVisit(new ChangeType("org.junit.BeforeClass", "org.junit.jupiter.api.BeforeAll", true).getVisitor()); doAfterVisit(new ChangeType("org.junit.AfterClass", "org.junit.jupiter.api.AfterAll", true).getVisitor()); - - return super.visitCompilationUnit(cu, ctx); + return tree; } } } diff --git a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java index 5b2e87923..5103fea4d 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java @@ -20,10 +20,12 @@ import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Issue; import org.openrewrite.java.JavaParser; +import org.openrewrite.kotlin.KotlinParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.kotlin.Assertions.kotlin; class AddParameterizedTestAnnotationTest implements RewriteTest { @Override @@ -31,6 +33,8 @@ public void defaults(RecipeSpec spec) { spec .parser(JavaParser.fromJavaVersion() .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "junit-jupiter-params-5.9")) + .parser(KotlinParser.builder() + .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "junit-jupiter-params-5.9")) .recipe(new AddParameterizedTestAnnotation()); } @@ -45,7 +49,7 @@ void replaceTestWithParameterizedTest() { import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; - + class NumbersTest { @Test @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @@ -58,7 +62,7 @@ void testIsOdd(int number) { import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; - + class NumbersTest { @ParameterizedTest @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @@ -67,6 +71,35 @@ void testIsOdd(int number) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.api.Test + import org.junit.jupiter.params.provider.ValueSource + import org.junit.jupiter.api.Assertions.assertTrue + + class NumbersTest { + @Test + @ValueSource(ints = [1, 3, 5, -3, 15, Int.MAX_VALUE]) + fun testIsOdd(number: Int) { + assertTrue(number % 2 != 0) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.ValueSource + import org.junit.jupiter.api.Assertions.assertTrue + + class NumbersTest { + @ParameterizedTest + @ValueSource(ints = [1, 3, 5, -3, 15, Int.MAX_VALUE]) + fun testIsOdd(number: Int) { + assertTrue(number % 2 != 0) + } + } + """ ) ); } diff --git a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java index 47d723d7a..2c71b6107 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java @@ -21,10 +21,12 @@ import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Issue; import org.openrewrite.java.JavaParser; +import org.openrewrite.kotlin.KotlinParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.kotlin.Assertions.kotlin; @SuppressWarnings("JUnitMalformedDeclaration") class UpdateBeforeAfterAnnotationsTest implements RewriteTest { @@ -34,6 +36,8 @@ public void defaults(RecipeSpec spec) { spec .parser(JavaParser.fromJavaVersion() .classpathFromResources(new InMemoryExecutionContext(), "junit-4.13")) + .parser(KotlinParser.builder() + .classpathFromResources(new InMemoryExecutionContext(), "junit-4.13")) .recipe(new UpdateBeforeAfterAnnotations()); } @@ -45,9 +49,8 @@ void beforeToBeforeEach() { java( """ import org.junit.Before; - + class Test { - @Before void before() { } @@ -55,14 +58,36 @@ void before() { """, """ import org.junit.jupiter.api.BeforeEach; - + class Test { - @BeforeEach void before() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.Before + + class Test { + + @Before + fun before() { + } + } + """, + """ + import org.junit.jupiter.api.BeforeEach + + class Test { + + @BeforeEach + fun before() { + } + } + """ ) ); } @@ -185,8 +210,8 @@ void before() { } @Test - @Disabled("Issue #59") - void beforeMethodOverridesPublicAbstract() { + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/59") + void retainPublicModifierOnOverriddenMethod() { //language=java rewriteRun(