2020import org .openrewrite .Preconditions ;
2121import org .openrewrite .Recipe ;
2222import org .openrewrite .TreeVisitor ;
23- import org .openrewrite .java .AnnotationMatcher ;
2423import org .openrewrite .java .JavaIsoVisitor ;
2524import org .openrewrite .java .JavaVisitor ;
2625import org .openrewrite .java .search .UsesType ;
27- import org .openrewrite .java .service .AnnotationService ;
2826import org .openrewrite .java .tree .J ;
2927import org .openrewrite .java .tree .JavaType ;
3028import org .openrewrite .java .tree .Statement ;
3432
3533public 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}
0 commit comments