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

JUnit4 SpringClassRule and SpringMethodRule to SpringExtension #571

Merged
merged 8 commits into from
Aug 14, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.spring.test;

import org.openrewrite.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;

import java.util.Comparator;

public class SpringRulesToJUnitExtension extends Recipe {
@Override
public @NlsRewrite.DisplayName String getDisplayName() {
return "Replace JUnit 4 `SpringClassRule` and `SpringMethodRule` with JUnit 5 `SpringExtension`";
}

@Override
public @NlsRewrite.Description String getDescription() {
return "Replace JUnit 4 `SpringClassRule` and `SpringMethodRule` with JUnit 5 `SpringExtension` or rely on an existing `@SpringBootTest`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(Preconditions.and(new UsesType<>("org.springframework.test.context.junit4.rules.SpringClassRule", true), new UsesType<>("org.springframework.test.context.junit4.rules.SpringMethodRule", true)), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext executionContext) {
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
if (multiVariable.getTypeExpression() instanceof J.Identifier) {
J.Identifier id = (J.Identifier) multiVariable.getTypeExpression();
if (id.getSimpleName().equals("SpringClassRule") || id.getSimpleName().equals("SpringMethodRule")) {
maybeRemoveImport("org.springframework.test.context.junit4.rules.SpringClassRule");
maybeRemoveImport("org.springframework.test.context.junit4.rules.SpringMethodRule");
maybeRemoveImport("org.junit.ClassRule");
maybeRemoveImport("org.junit.Rule");
doAfterVisit(new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
if (classDecl.getLeadingAnnotations().stream().noneMatch(ann -> ann.getSimpleName().equals("SpringBootTest") || ann.getSimpleName().equals("ExtendWith"))) {
maybeAddImport("org.junit.jupiter.api.extension.ExtendWith");
maybeAddImport("org.springframework.test.context.junit.jupiter.SpringExtension");
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
return JavaTemplate.builder("@ExtendWith(SpringExtension.class)")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(executionContext, "junit-jupiter-api", "spring-test"))
.imports("org.junit.jupiter.api.extension.ExtendWith", "org.springframework.test.context.junit.jupiter.SpringExtension")
.build().apply(getCursor(), classDecl.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
}
return super.visitClassDeclaration(classDecl, executionContext);
}
});
return null;
}
}
return super.visitVariableDeclarations(multiVariable, executionContext);
}
});
}
}
Binary file not shown.
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/spring-boot-24.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ recipeList:
artifactId: spring-boot-tools
version: 2.x
onlyIfUsing: org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathExtension
- org.openrewrite.java.spring.test.SpringRulesToJUnitExtension
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.spring.boot2.UnnecessarySpringRunWith
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.spring.test;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class SpringRulesToJUnitExtensionTest implements RewriteTest {


@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new SpringRulesToJUnitExtension())
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-boot-test", "spring-test", "junit-4"));
}

@Test
@DocumentExample
void migrateSpringRulesToJUnit() {
rewriteRun(
//language=java
java(
"""
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import org.junit.ClassRule;
import org.junit.Rule;

@SpringBootTest
class SomeTest {

@ClassRule
public static final SpringClassRule springClassRule = new SpringClassRule();

@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();

}
""",
"""
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SomeTest {

}
"""
)
);
}

@Test
void migrateSpringRulesToJUnit_2() {
rewriteRun(
//language=java
java(
"""
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import org.junit.ClassRule;
import org.junit.Rule;

class SomeTest {

@ClassRule
public static final SpringClassRule springClassRule = new SpringClassRule();

@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();

}
""",
"""
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
class SomeTest {

}
"""
)
);
}

}
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
Loading