Skip to content

Commit 1bf96d3

Browse files
committed
Extend JUnit4to5Migration for OkHttp MockWebServer
The OkHttp `MockWebServer` JUnit 4 rule also exists as a JUnit Jupiter extension.
1 parent 8405568 commit 1bf96d3

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/main/resources/META-INF/rewrite/junit5.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ recipeList:
7979
- org.openrewrite.java.testing.junit5.EnclosedToNested
8080
- org.openrewrite.java.testing.junit5.AddMissingNested
8181
- org.openrewrite.java.testing.hamcrest.AddHamcrestIfUsed
82+
- org.openrewrite.java.testing.junit5.UpgradeOkHttpMockWebServer
8283
- org.openrewrite.maven.RemoveDependency:
8384
groupId: junit
8485
artifactId: junit
@@ -212,6 +213,25 @@ recipeList:
212213
acceptTransitive: true
213214
---
214215
type: specs.openrewrite.org/v1beta/recipe
216+
name: org.openrewrite.java.testing.junit5.UpgradeOkHttpMockWebServer
217+
displayName: Use OkHttp 3 MockWebServer for JUnit 5
218+
description: Migrates OkHttp 3 `MockWebServer` to the JUnit Jupiter compatible version.
219+
tags:
220+
- testing
221+
- junit
222+
- okhttp
223+
recipeList:
224+
- org.openrewrite.java.ChangePackage:
225+
oldPackageName: okhttp3.mockwebserver
226+
newPackageName: mockwebserver3
227+
recursive: true
228+
- org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
229+
oldGroupId: com.squareup.okhttp3
230+
oldArtifactId: mockwebserver
231+
newArtifactId: mockwebserver3-junit5
232+
newVersion: 5.0.0-alpha.11
233+
---
234+
type: specs.openrewrite.org/v1beta/recipe
215235
name: org.openrewrite.java.testing.junit5.CleanupAssertions
216236
displayName: Clean Up Assertions
217237
description: Simplifies JUnit Jupiter assertions to their most-direct equivalents
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.junit5;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.config.Environment;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
import org.openrewrite.test.TypeValidation;
23+
24+
import static org.openrewrite.java.Assertions.java;
25+
import static org.openrewrite.java.Assertions.mavenProject;
26+
import static org.openrewrite.maven.Assertions.pomXml;
27+
28+
public class UpgradeOkHttpMockWebServerTest implements RewriteTest {
29+
30+
@Override
31+
public void defaults(RecipeSpec spec) {
32+
spec
33+
.typeValidationOptions(TypeValidation.none())
34+
.recipe(Environment.builder()
35+
.scanRuntimeClasspath()
36+
.build()
37+
.activateRecipes("org.openrewrite.java.testing.junit5.UpgradeOkHttpMockWebServer"));
38+
}
39+
40+
@Test
41+
void shouldUpgradeMavenDependency() {
42+
rewriteRun(
43+
mavenProject("project",
44+
//language=java
45+
java(
46+
"""
47+
import okhttp3.mockwebserver.MockWebServer;
48+
49+
class Test {
50+
void test() {
51+
MockWebServer server = new MockWebServer();
52+
}
53+
}
54+
""",
55+
"""
56+
import mockwebserver3.MockWebServer;
57+
58+
class Test {
59+
void test() {
60+
MockWebServer server = new MockWebServer();
61+
}
62+
}
63+
"""
64+
),
65+
//language=xml
66+
pomXml(
67+
"""
68+
<project>
69+
<modelVersion>4.0.0</modelVersion>
70+
<groupId>com.example</groupId>
71+
<artifactId>demo</artifactId>
72+
<version>0.0.1-SNAPSHOT</version>
73+
<dependencies>
74+
<dependency>
75+
<groupId>com.squareup.okhttp3</groupId>
76+
<artifactId>mockwebserver</artifactId>
77+
<version>4.10.0</version>
78+
</dependency>
79+
</dependencies>
80+
</project>
81+
""",
82+
"""
83+
<project>
84+
<modelVersion>4.0.0</modelVersion>
85+
<groupId>com.example</groupId>
86+
<artifactId>demo</artifactId>
87+
<version>0.0.1-SNAPSHOT</version>
88+
<dependencies>
89+
<dependency>
90+
<groupId>com.squareup.okhttp3</groupId>
91+
<artifactId>mockwebserver3-junit5</artifactId>
92+
<version>5.0.0-alpha.11</version>
93+
</dependency>
94+
</dependencies>
95+
</project>
96+
"""
97+
)
98+
)
99+
);
100+
}
101+
}

0 commit comments

Comments
 (0)