Skip to content

Commit 20cca13

Browse files
committed
Only match methods with test annotations
Fixes #874
1 parent 9803871 commit 20cca13

3 files changed

Lines changed: 40 additions & 11 deletions

File tree

src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ private boolean methodExists(JavaType.Method method, String newName) {
170170
private static boolean hasJUnit5MethodAnnotation(MethodDeclaration method) {
171171
for (J.Annotation a : method.getLeadingAnnotations()) {
172172
if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test") ||
173-
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate") ||
174173
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest") ||
175174
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest") ||
176-
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory")) {
175+
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory") ||
176+
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate")) {
177177
return true;
178178
}
179179
}

src/main/java/org/openrewrite/java/testing/cleanup/TestMethodsShouldBeVoid.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
import org.openrewrite.Preconditions;
2121
import org.openrewrite.Recipe;
2222
import org.openrewrite.TreeVisitor;
23-
import org.openrewrite.java.AnnotationMatcher;
2423
import org.openrewrite.java.JavaIsoVisitor;
2524
import org.openrewrite.java.JavaVisitor;
2625
import org.openrewrite.java.search.UsesType;
27-
import org.openrewrite.java.service.AnnotationService;
2826
import org.openrewrite.java.tree.J;
2927
import org.openrewrite.java.tree.JavaType;
3028
import org.openrewrite.java.tree.Statement;
@@ -34,7 +32,7 @@
3432

3533
public class TestMethodsShouldBeVoid extends Recipe {
3634

37-
private static final String TEST_ANNOTATION_PATTERN = "org..* *Test*";
35+
private static final String TEST_ANNOTATION_TYPE_PATTERN = "org..*Test";
3836

3937
@Override
4038
public String getDisplayName() {
@@ -50,13 +48,11 @@ public String getDescription() {
5048

5149
@Override
5250
public TreeVisitor<?, ExecutionContext> getVisitor() {
53-
return Preconditions.check(new UsesType<>(TEST_ANNOTATION_PATTERN, true), new JavaIsoVisitor<ExecutionContext>() {
51+
return Preconditions.check(new UsesType<>(TEST_ANNOTATION_TYPE_PATTERN, true), new JavaIsoVisitor<ExecutionContext>() {
5452
@Override
5553
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
5654
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
57-
58-
// Only consider test methods
59-
if (!service(AnnotationService.class).matches(getCursor(), new AnnotationMatcher(TEST_ANNOTATION_PATTERN, true))) {
55+
if (!hasJUnit5MethodAnnotation(m)) {
6056
return m;
6157
}
6258

@@ -105,4 +101,17 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
105101
null;
106102
}
107103
}
104+
105+
private static boolean hasJUnit5MethodAnnotation(J.MethodDeclaration method) {
106+
for (J.Annotation a : method.getLeadingAnnotations()) {
107+
if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test") ||
108+
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest") ||
109+
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest") ||
110+
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory") ||
111+
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate")) {
112+
return true;
113+
}
114+
}
115+
return false;
116+
}
108117
}

src/test/java/org/openrewrite/java/testing/cleanup/TestMethodsShouldBeVoidTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.InMemoryExecutionContext;
21+
import org.openrewrite.Issue;
2122
import org.openrewrite.java.JavaParser;
2223
import org.openrewrite.test.RecipeSpec;
2324
import org.openrewrite.test.RewriteTest;
@@ -30,8 +31,8 @@ class TestMethodsShouldBeVoidTest implements RewriteTest {
3031
@Override
3132
public void defaults(RecipeSpec spec) {
3233
spec
33-
.parser(JavaParser.fromJavaVersion()
34-
.classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5", "junit-jupiter-params-5"))
34+
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),
35+
"junit-jupiter-api-5", "junit-jupiter-params-5"))
3536
.recipe(new TestMethodsShouldBeVoid());
3637
}
3738

@@ -355,4 +356,23 @@ private int calculateValue() {
355356
)
356357
);
357358
}
359+
360+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/874")
361+
@Test
362+
void doesNotMatchNullableAnnotation() {
363+
rewriteRun(
364+
spec -> spec.parser(JavaParser.fromJavaVersion().classpath("jspecify")),
365+
//language=java
366+
java(
367+
"""
368+
import org.jspecify.annotations.Nullable;
369+
370+
public interface Doer {
371+
@Nullable
372+
String getFile(String input);
373+
}
374+
"""
375+
)
376+
);
377+
}
358378
}

0 commit comments

Comments
 (0)