Skip to content

Commit

Permalink
Add Kotlin support for JUnit recipes: UpdateBeforeAfterAnnotations (#533
Browse files Browse the repository at this point in the history
)

* Adding Kotlin support to the recipes, fixing the recipes to work use preVisit instead of visitCompilationUnit.

* Do a single recipe run per unit test

* Stop after pre visit, since we're only updating imports

* Adding Kotlin support for the UpdateBeforeAfterAnnotations, along with new tests for kotlin

* Added kotlin tests for AddParameterizedTestAnnotation

* Add Picnic AssertJ rules to AssertJ best practices (#527)

* Add Picnic AssertJ rules to AssertJ best practices

* Include Picnic's JUnitToAssertJRulesRecipes in migration

* Exclude `jakarta.xml.bind-api` from TimeFold

* Move the exclude to rewrite-third-party

* refactor: Only publish build scans if authenticated

Use this link to re-run the recipe: https://app.moderne.io/recipes/builder/kLJjXlflM?organizationId=T3BlblJld3JpdGU%3D

Co-authored-by: Moderne <team@moderne.io>

* Drop Java 17 requirement through rewrite-third-party (#531)

* Jmockit Expectations with no times or result should be transformed to Mockito verify statement (#530)

* Ensure Jmockit expectations with no times or result transform to a mockito verify

* Minor polish to text blocks

* Make times, minTimes, maxTimes more flexible as it was originally so even if someone mistakenly puts times and minTimes together, it still migrates without issue

---------

Co-authored-by: Tim te Beek <tim@moderne.io>

* Rewrite both JMockit  `@Mocked` and `@Injectable` annotated arguments (#532)

* Ensure Jmockit expectations with no times or result transform to a mockito verify

* Minor polish to text blocks

* Make times, minTimes, maxTimes more flexible as it was originally so even if someone mistakenly puts times and minTimes together, it still migrates without issue

* Add feature to enable migration of Jmockit Injectable annotation exactly as we are doing the Mocked annotation ie method parameter annotation as well as field annotation. Have moved all of the code from JMockitMockedVariableToMockito to JMockitAnnotationToMockito for code reuse. Also add the corresponding test cases and jmockit.yml file modification.

* Use `@Option` instead of inheritance

* Drop Option and just replace both variants

* Update display name and description to match application

---------

Co-authored-by: Tim te Beek <tim@moderne.io>

* Adding Kotlin support for the UpdateBeforeAfterAnnotations, along with new tests for kotlin

* Added kotlin tests for AddParameterizedTestAnnotation

* Restoring changes to issue to retain the references

* Remove trailing whitespace in text blocks

* Update src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java

Co-authored-by: Tim te Beek <timtebeek@gmail.com>

* Removing tests for kotlin and keep one in the Documentation example. We can tackle the issues and add tests separately if we see bug in workflows.

* Removing extra tests for Kotlin, and keeping one, fixing issue with failing build.

* Restored changes to the disabled test.

* Minor touch up

---------

Co-authored-by: Amitoj Duggal <a.duggal@mytaxi.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
Co-authored-by: Tim te Beek <timtebeek@gmail.com>
Co-authored-by: Moderne <team@moderne.io>
Co-authored-by: Shivani Sharma <s.happyrose@gmail.com>
  • Loading branch information
6 people authored Jun 23, 2024
1 parent 8388b28 commit 9eabd19
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public static class UpdateBeforeAfterAnnotationsVisitor extends JavaIsoVisitor<ExecutionContext> {

@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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@
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
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());
}

Expand All @@ -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})
Expand All @@ -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})
Expand All @@ -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)
}
}
"""
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}

Expand All @@ -45,24 +49,45 @@ void beforeToBeforeEach() {
java(
"""
import org.junit.Before;
class Test {
@Before
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() {
}
}
"""
)
);
}
Expand Down Expand Up @@ -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(

Expand Down

0 comments on commit 9eabd19

Please sign in to comment.