Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kotlin support for JUnit recipes: UpdateBeforeAfterAnnotations #533

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0d54471
Adding Kotlin support to the recipes, fixing the recipes to work use …
Jun 17, 2024
6abf2f3
Merge branch 'main' into feature/add-kotlin-support-junit-cleanuprule
amitojduggal Jun 17, 2024
30960c1
Do a single recipe run per unit test
timtebeek Jun 17, 2024
e07c523
Stop after pre visit, since we're only updating imports
timtebeek Jun 17, 2024
15545f1
Adding Kotlin support for the UpdateBeforeAfterAnnotations, along wit…
Jun 17, 2024
fa13722
Added kotlin tests for AddParameterizedTestAnnotation
Jun 18, 2024
f6fd008
Add Picnic AssertJ rules to AssertJ best practices (#527)
timtebeek Jun 17, 2024
64dfb10
refactor: Only publish build scans if authenticated
timtebeek Jun 18, 2024
e579694
Drop Java 17 requirement through rewrite-third-party (#531)
timtebeek Jun 19, 2024
1d22c17
Jmockit Expectations with no times or result should be transformed to…
shivanisky Jun 20, 2024
77af97e
Rewrite both JMockit `@Mocked` and `@Injectable` annotated arguments…
shivanisky Jun 20, 2024
c88a886
Adding Kotlin support for the UpdateBeforeAfterAnnotations, along wit…
Jun 17, 2024
df099f0
Added kotlin tests for AddParameterizedTestAnnotation
Jun 18, 2024
1d8d535
Merge remote-tracking branch 'origin/feature/add-kotlin-support-junit…
Jun 20, 2024
ddd9a1e
Restoring changes to issue to retain the references
Jun 20, 2024
a1eba6b
Remove trailing whitespace in text blocks
timtebeek Jun 20, 2024
b42b321
Update src/test/java/org/openrewrite/java/testing/junit5/UpdateBefore…
amitojduggal Jun 21, 2024
8423915
Removing tests for kotlin and keep one in the Documentation example. …
Jun 21, 2024
2799ad2
Removing extra tests for Kotlin, and keeping one, fixing issue with f…
Jun 21, 2024
6049913
Restored changes to the disabled test.
Jun 21, 2024
41e97e6
Minor touch up
timtebeek Jun 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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