Skip to content

Commit 97996ce

Browse files
authored
Only turn the WireMockRule field's @Rule into @RegisterExtension in UseWiremockExtension (#1218)
`UseWiremockExtension` queued a file-wide `ChangeType` from `org.junit.Rule` to `RegisterExtension` as soon as it saw a `new WireMockRule(..)`, so every other rule in the class was retyped too. A `@Rule TemporaryFolder tmp` next to the WireMock rule became `@RegisterExtension TemporaryFolder tmp`, which Jupiter rejects at runtime, and `TemporaryFolderToTempDir` and `TestRuleToTestInfo` later in `JUnit4to5Migration` no longer recognised those fields. Now only the `@Rule` on a `WireMockRule` field is replaced.
1 parent 06491a7 commit 97996ce

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

‎src/main/java/org/openrewrite/java/testing/junit5/UseWiremockExtension.java‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openrewrite.Preconditions;
2121
import org.openrewrite.Recipe;
2222
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.internal.ListUtils;
2324
import org.openrewrite.java.*;
2425
import org.openrewrite.java.search.UsesType;
2526
import org.openrewrite.java.tree.*;
@@ -28,7 +29,9 @@ public class UseWiremockExtension extends Recipe {
2829

2930
private static final String WIREMOCK_RULE = "com.github.tomakehurst.wiremock.junit.WireMockRule";
3031
private static final String WIREMOCK_EXTENSION = "com.github.tomakehurst.wiremock.junit5.WireMockExtension";
32+
private static final String REGISTER_EXTENSION = "org.junit.jupiter.api.extension.RegisterExtension";
3133
private static final MethodMatcher newWiremockRule = new MethodMatcher(WIREMOCK_RULE + " <constructor>(..)");
34+
private static final AnnotationMatcher RULE = new AnnotationMatcher("@org.junit.Rule");
3235

3336
@Getter
3437
final String displayName = "Use wiremock extension";
@@ -48,6 +51,19 @@ public J preVisit(J tree, ExecutionContext ctx) {
4851
return tree;
4952
}
5053

54+
@Override
55+
public J visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
56+
J.VariableDeclarations vd = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, ctx);
57+
if (multiVariable.getVariables().stream().noneMatch(v -> TypeUtils.isOfClassType(v.getType(), WIREMOCK_RULE))) {
58+
return vd;
59+
}
60+
maybeRemoveImport("org.junit.Rule");
61+
maybeAddImport(REGISTER_EXTENSION);
62+
return vd.withLeadingAnnotations(ListUtils.map(vd.getLeadingAnnotations(), a -> RULE.matches(a) ?
63+
a.withAnnotationType(TypeTree.<J.Identifier>build("RegisterExtension").withType(JavaType.ShallowClass.build(REGISTER_EXTENSION)).withPrefix(a.getAnnotationType().getPrefix())) :
64+
a));
65+
}
66+
5167
@Override
5268
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
5369
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
@@ -65,8 +81,6 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
6581
J.NewClass n = (J.NewClass) super.visitNewClass(newClass, ctx);
6682
if (newWiremockRule.matches(n)) {
6783
maybeAddImport(WIREMOCK_EXTENSION);
68-
doAfterVisit(new ChangeType("org.junit.Rule", "org.junit.jupiter.api.extension.RegisterExtension", true)
69-
.getVisitor());
7084

7185
Expression arg = n.getArguments().get(0);
7286

‎src/test/java/org/openrewrite/java/testing/junit5/UseWiremockExtensionTest.java‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,41 @@ String secureBaseUrl() {
206206
);
207207
}
208208

209+
@Test
210+
void otherRulesKeepTheirAnnotation() {
211+
//language=java
212+
rewriteRun(
213+
java(
214+
"""
215+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
216+
import org.junit.Rule;
217+
import org.junit.rules.TemporaryFolder;
218+
219+
class Test {
220+
@Rule
221+
public WireMockRule wm = new WireMockRule(7001);
222+
@Rule
223+
public TemporaryFolder tmp = new TemporaryFolder();
224+
}
225+
""",
226+
"""
227+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
228+
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
229+
import org.junit.Rule;
230+
import org.junit.jupiter.api.extension.RegisterExtension;
231+
import org.junit.rules.TemporaryFolder;
232+
233+
class Test {
234+
@RegisterExtension
235+
public WireMockExtension wm = WireMockExtension.newInstance().options(WireMockConfiguration.options().port(7001)).build();
236+
@Rule
237+
public TemporaryFolder tmp = new TemporaryFolder();
238+
}
239+
"""
240+
)
241+
);
242+
}
243+
209244
@Nested
210245
class InJUnit4to5Migration implements RewriteTest {
211246

@@ -293,6 +328,42 @@ public void token() {
293328
);
294329
}
295330

331+
@Test
332+
void temporaryFolderNextToWireMockRuleBecomesTempDir() {
333+
//language=java
334+
rewriteRun(
335+
java(
336+
"""
337+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
338+
import org.junit.Rule;
339+
import org.junit.rules.TemporaryFolder;
340+
341+
class WireMockTest {
342+
@Rule
343+
public WireMockRule wm = new WireMockRule(7001);
344+
@Rule
345+
public TemporaryFolder tmp = new TemporaryFolder();
346+
}
347+
""",
348+
"""
349+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
350+
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
351+
import org.junit.jupiter.api.extension.RegisterExtension;
352+
import org.junit.jupiter.api.io.TempDir;
353+
354+
import java.io.File;
355+
356+
class WireMockTest {
357+
@RegisterExtension
358+
public WireMockExtension wm = WireMockExtension.newInstance().options(WireMockConfiguration.options().port(7001)).build();
359+
@TempDir
360+
public File tmp;
361+
}
362+
"""
363+
)
364+
);
365+
}
366+
296367
@Test
297368
void stoppedRuleBecomesServer() {
298369
//language=java

0 commit comments

Comments
 (0)