From 763f40e6e7162f08467197d58e64bf5a9393e7fa Mon Sep 17 00:00:00 2001 From: martinfrancois Date: Wed, 12 Aug 2026 19:53:33 +0200 Subject: [PATCH] Respect composed JUnit test templates --- .../AddParameterizedTestAnnotation.java | 3 +- .../AddParameterizedTestAnnotationTest.java | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotation.java b/src/main/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotation.java index 75793dadb..51838ba37 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotation.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotation.java @@ -33,6 +33,7 @@ public class AddParameterizedTestAnnotation extends Recipe { private static final AnnotationMatcher TEST_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.Test"); private static final AnnotationMatcher PARAM_TEST_MATCHER = new AnnotationMatcher("@org.junit.jupiter.params.ParameterizedTest"); + private static final AnnotationMatcher TEST_TEMPLATE_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.TestTemplate", true); private static final List SOURCE_ANNOTATIONS = Stream.of( "ValueSource", "CsvSource", @@ -63,8 +64,8 @@ private static class AnnotatedMethodVisitor extends JavaIsoVisitor SOURCE_ANNOTATIONS.stream().anyMatch(matcher -> matcher.matches(ann)))) { return m; } diff --git a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java index 21e74fc32..0303ffaa6 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java @@ -500,4 +500,73 @@ public Stream provideArguments(ExtensionContext context) { ) ); } + + @Test + void doesNotAddParameterizedTestToComposedTestTemplates() { + rewriteRun( + //language=java + java( + """ + package direct; + + import java.lang.annotation.Retention; + import java.lang.annotation.Target; + + import org.junit.jupiter.api.TestTemplate; + import org.junit.jupiter.params.provider.ValueSource; + + import static java.lang.annotation.ElementType.METHOD; + import static java.lang.annotation.RetentionPolicy.RUNTIME; + + @Retention(RUNTIME) + @Target(METHOD) + @TestTemplate + @interface FuzzTest { + } + + class Test { + @FuzzTest + @ValueSource(strings = "input") + void fuzz(String input) { + } + } + """ + ), + //language=java + java( + """ + package transitive; + + import java.lang.annotation.Retention; + import java.lang.annotation.Target; + + import org.junit.jupiter.api.TestTemplate; + import org.junit.jupiter.params.provider.ValueSource; + + import static java.lang.annotation.ElementType.ANNOTATION_TYPE; + import static java.lang.annotation.ElementType.METHOD; + import static java.lang.annotation.RetentionPolicy.RUNTIME; + + @Retention(RUNTIME) + @Target(ANNOTATION_TYPE) + @TestTemplate + @interface ComposedTestTemplate { + } + + @Retention(RUNTIME) + @Target(METHOD) + @ComposedTestTemplate + @interface FuzzTest { + } + + class Test { + @FuzzTest + @ValueSource(strings = "input") + void fuzz(String input) { + } + } + """ + ) + ); + } }