Skip to content

Commit fd6cf53

Browse files
amishra-utimtebeek
andauthored
Fix: Don't modify regular test to parameterized test incorrectly (#707)
* Fix: Don't modify regular test to parameterzied incorrectly * Slight polish to avoid long line --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 3f40f60 commit fd6cf53

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/main/java/org/openrewrite/java/testing/junit5/JUnitParamsRunnerToParameterized.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class JUnitParamsRunnerToParameterized extends Recipe {
5050

5151
private static final String INIT_METHOD_REFERENCES = "init-method-references";
5252
private static final String PARAMETERS_FOR_PREFIX = "parametersFor";
53+
private static final String PARAMETERIZED_TESTS = "parameterized-tests";
5354
private static final String INIT_METHODS_MAP = "named-parameters-map";
5455
private static final String CONVERSION_NOT_SUPPORTED = "conversion-not-supported";
5556

@@ -82,6 +83,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
8283
doAfterVisit(new ParametersNoArgsImplicitMethodSource(initMethods,
8384
getCursor().computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<>()),
8485
getCursor().computeMessageIfAbsent(CONVERSION_NOT_SUPPORTED, v -> new HashSet<>()),
86+
getCursor().computeMessageIfAbsent(PARAMETERIZED_TESTS, v -> new HashSet<>()),
8587
ctx));
8688
}
8789
return cd;
@@ -103,6 +105,8 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ct
103105
J.Annotation anno = super.visitAnnotation(annotation, ctx);
104106
Cursor classDeclCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
105107
if (PARAMETERS_MATCHER.matches(anno)) {
108+
classDeclCursor.computeMessageIfAbsent(PARAMETERIZED_TESTS, v -> new HashSet<>())
109+
.add(getCursor().firstEnclosing(J.MethodDeclaration.class).getSimpleName());
106110
String annotationArgumentValue = getAnnotationArgumentForInitMethod(anno, "method", "named");
107111
if (annotationArgumentValue != null) {
108112
for (String method : annotationArgumentValue.split(",")) {
@@ -178,16 +182,18 @@ private static class ParametersNoArgsImplicitMethodSource extends JavaIsoVisitor
178182

179183
private final Set<String> initMethods;
180184
private final Set<String> unsupportedConversions;
185+
private final Set<String> parameterizedTests;
181186
private final Map<String, String> initMethodReferences;
182187

183188
private final JavaTemplate parameterizedTestTemplate;
184189
private final JavaTemplate parameterizedTestTemplateWithName;
185190
private final JavaTemplate methodSourceTemplate;
186191

187-
public ParametersNoArgsImplicitMethodSource(Set<String> initMethods, Map<String, String> initMethodReferences, Set<String> unsupportedConversions, ExecutionContext ctx) {
192+
public ParametersNoArgsImplicitMethodSource(Set<String> initMethods, Map<String, String> initMethodReferences, Set<String> unsupportedConversions, Set<String> parameterizedTests, ExecutionContext ctx) {
188193
this.initMethods = initMethods;
189194
this.initMethodReferences = initMethodReferences;
190195
this.unsupportedConversions = unsupportedConversions;
196+
this.parameterizedTests = parameterizedTests;
191197

192198
// build @ParameterizedTest template
193199
JavaParser.Builder<?, ?> javaParser = JavaParser.fromJavaVersion()
@@ -254,6 +260,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
254260

255261
private J.Annotation maybeReplaceTestAnnotation(Cursor anno, @Nullable String parameterizedTestArgument) {
256262
if (JUPITER_TEST_ANNOTATION_MATCHER.matches(anno.getValue()) || JUNIT_TEST_ANNOTATION_MATCHER.matches(anno.getValue())) {
263+
if (!parameterizedTests.contains(anno.firstEnclosing(J.MethodDeclaration.class).getSimpleName())) {
264+
return anno.getValue();
265+
}
257266
if (parameterizedTestArgument == null) {
258267
return parameterizedTestTemplate.apply(anno, ((J.Annotation) anno.getValue()).getCoordinates().replace());
259268
} else {

src/test/java/org/openrewrite/java/testing/junit5/JUnitParamsRunnerToParameterizedTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ public void personIsChild(int age, boolean valid) {
6767
private Object[] parametersForPersonIsChild() {
6868
return new Object[]{new Object[]{3, false}, new Object[]{7, false}};
6969
}
70+
71+
@Test
72+
public void regularTest() {}
7073
}
7174
""",
7275
"""
76+
import org.junit.Test;
7377
import org.junit.jupiter.params.ParameterizedTest;
7478
import org.junit.jupiter.params.provider.MethodSource;
7579
@@ -92,6 +96,9 @@ public void personIsChild(int age, boolean valid) {
9296
private static Object[] parametersForPersonIsChild() {
9397
return new Object[]{new Object[]{3, false}, new Object[]{7, false}};
9498
}
99+
100+
@Test
101+
public void regularTest() {}
95102
}
96103
"""
97104
)

0 commit comments

Comments
 (0)