Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -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<AnnotationMatcher> SOURCE_ANNOTATIONS = Stream.of(
"ValueSource",
"CsvSource",
Expand Down Expand Up @@ -63,8 +64,8 @@ private static class AnnotatedMethodVisitor extends JavaIsoVisitor<ExecutionCont
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(md, ctx);

// Return early if already annotated with @ParameterizedTest or not annotated with any @...Source annotation
if (m.getLeadingAnnotations().stream().anyMatch(PARAM_TEST_MATCHER::matches) ||
m.getLeadingAnnotations().stream().anyMatch(TEST_TEMPLATE_MATCHER::matches) ||
m.getLeadingAnnotations().stream().noneMatch(ann -> SOURCE_ANNOTATIONS.stream().anyMatch(matcher -> matcher.matches(ann)))) {
return m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,73 @@ public Stream<? extends Arguments> 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) {
}
}
"""
)
);
}
}
Loading