Skip to content

Commit 3dd349c

Browse files
Do not ever exclude junit if Testcontainers is used (#746)
* Do not ever exclude junit if Testcontainers is used * Slight polish to remove left over comments --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent c729084 commit 3dd349c

File tree

2 files changed

+165
-92
lines changed

2 files changed

+165
-92
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,7 @@ recipeList:
102102
- org.openrewrite.java.dependencies.RemoveDependency:
103103
groupId: junit
104104
artifactId: junit
105-
- org.openrewrite.maven.ExcludeDependency:
106-
groupId: junit
107-
artifactId: junit
108-
# Workaround for https://github.com/testcontainers/testcontainers-java/issues/970:
109-
- org.openrewrite.maven.RemoveExclusion:
110-
groupId: org.testcontainers
111-
artifactId: '*'
112-
exclusionGroupId: junit
113-
exclusionArtifactId: junit
114-
# Similar for https://github.com/openrewrite/rewrite-testing-frameworks/issues/477
115-
- org.openrewrite.maven.RemoveExclusion:
116-
groupId: org.springframework.boot
117-
artifactId: spring-boot-testcontainers
118-
exclusionGroupId: junit
119-
exclusionArtifactId: junit
105+
- org.openrewrite.java.testing.junit5.ExcludeJUnit4UnlessUsingTestcontainers
120106
- org.openrewrite.java.dependencies.RemoveDependency:
121107
groupId: org.junit.vintage
122108
artifactId: junit-vintage-engine
@@ -145,7 +131,19 @@ recipeList:
145131
newFullyQualifiedTypeName: org.jbehave.core.junit.JupiterStories
146132
- org.openrewrite.java.testing.arquillian.ArquillianJUnit4ToArquillianJUnit5
147133
- org.openrewrite.java.testing.dbrider.MigrateDbRiderSpringToDbRiderJUnit5
148-
134+
---
135+
type: specs.openrewrite.org/v1beta/recipe
136+
name: org.openrewrite.java.testing.junit5.ExcludeJUnit4UnlessUsingTestcontainers
137+
displayName: Exclude JUnit 4, unless Testcontainers is used
138+
description: Excludes JUnit 4, as it ought not to be necessary in a JUnit 5 project, unless Testcontainers is used.
139+
preconditions:
140+
- org.openrewrite.maven.search.DoesNotIncludeDependency:
141+
groupId: org.testcontainers
142+
artifactId: '*'
143+
recipeList:
144+
- org.openrewrite.maven.ExcludeDependency:
145+
groupId: junit
146+
artifactId: junit
149147
---
150148
type: specs.openrewrite.org/v1beta/recipe
151149
name: org.openrewrite.java.testing.junit5.UseHamcrestAssertThat

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

Lines changed: 151 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -157,95 +157,170 @@ void upgradeMavenPluginVersions() {
157157
);
158158
}
159159

160+
@Test
161+
void excludeJunit4Dependency() {
162+
// Just using play-test_2.13 as an example because it appears to still depend on junit.
163+
// In practice, this would probably just break it, I assume.
164+
//language=xml
165+
rewriteRun(
166+
pomXml(
167+
"""
168+
<project>
169+
<modelVersion>4.0.0</modelVersion>
170+
<parent>
171+
<groupId>org.springframework.boot</groupId>
172+
<artifactId>spring-boot-starter-parent</artifactId>
173+
<version>3.2.1</version>
174+
<relativePath/> <!-- lookup parent from repository -->
175+
</parent>
176+
<groupId>dev.ted</groupId>
177+
<artifactId>needs-exclusion</artifactId>
178+
<version>0.0.1</version>
179+
<dependencies>
180+
<dependency>
181+
<groupId>org.springframework.boot</groupId>
182+
<artifactId>spring-boot-starter</artifactId>
183+
</dependency>
184+
<dependency>
185+
<groupId>com.typesafe.play</groupId>
186+
<artifactId>play-test_2.13</artifactId>
187+
<version>2.9.6</version>
188+
<scope>test</scope>
189+
</dependency>
190+
</dependencies>
191+
</project>
192+
""",
193+
"""
194+
<project>
195+
<modelVersion>4.0.0</modelVersion>
196+
<parent>
197+
<groupId>org.springframework.boot</groupId>
198+
<artifactId>spring-boot-starter-parent</artifactId>
199+
<version>3.2.1</version>
200+
<relativePath/> <!-- lookup parent from repository -->
201+
</parent>
202+
<groupId>dev.ted</groupId>
203+
<artifactId>needs-exclusion</artifactId>
204+
<version>0.0.1</version>
205+
<dependencies>
206+
<dependency>
207+
<groupId>org.springframework.boot</groupId>
208+
<artifactId>spring-boot-starter</artifactId>
209+
</dependency>
210+
<dependency>
211+
<groupId>com.typesafe.play</groupId>
212+
<artifactId>play-test_2.13</artifactId>
213+
<version>2.9.6</version>
214+
<scope>test</scope>
215+
<exclusions>
216+
<exclusion>
217+
<groupId>junit</groupId>
218+
<artifactId>junit</artifactId>
219+
</exclusion>
220+
</exclusions>
221+
</dependency>
222+
</dependencies>
223+
</project>
224+
"""
225+
)
226+
);
227+
}
228+
160229
@Test
161230
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
162-
void dontExcludeJunit4DependencyfromTestcontainers() {
231+
void dontExcludeJunit4DependencyFromTestcontainers() {
163232
//language=xml
164-
String before = """
165-
<project>
166-
<modelVersion>4.0.0</modelVersion>
167-
<groupId>com.example.jackson</groupId>
168-
<artifactId>test-plugins</artifactId>
169-
<version>1.0.0</version>
170-
<dependencies>
171-
<dependency>
172-
<groupId>org.testcontainers</groupId>
173-
<artifactId>testcontainers</artifactId>
174-
<version>1.18.3</version>
175-
<scope>test</scope>
176-
</dependency>
177-
</dependencies>
178-
</project>
179-
""";
180-
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
181-
rewriteRun(pomXml(before, before));
233+
rewriteRun(
234+
pomXml(
235+
"""
236+
<project>
237+
<modelVersion>4.0.0</modelVersion>
238+
<groupId>com.example.jackson</groupId>
239+
<artifactId>test-plugins</artifactId>
240+
<version>1.0.0</version>
241+
<dependencies>
242+
<dependency>
243+
<groupId>org.testcontainers</groupId>
244+
<artifactId>testcontainers</artifactId>
245+
<version>1.18.3</version>
246+
<scope>test</scope>
247+
</dependency>
248+
</dependencies>
249+
</project>
250+
"""
251+
)
252+
);
182253
}
183254

184255
@Test
185256
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
186-
void dontExcludeJunit4DependencyfromTestcontainersJupiter() {
257+
void dontExcludeJunit4DependencyFromTestcontainersJupiter() {
187258
//language=xml
188-
String before = """
189-
<project>
190-
<modelVersion>4.0.0</modelVersion>
191-
<groupId>com.example.jackson</groupId>
192-
<artifactId>test-plugins</artifactId>
193-
<version>1.0.0</version>
194-
<dependencies>
195-
<dependency>
196-
<groupId>org.testcontainers</groupId>
197-
<artifactId>junit-jupiter</artifactId>
198-
<version>1.18.3</version>
199-
<scope>test</scope>
200-
</dependency>
201-
</dependencies>
202-
</project>
203-
""";
204-
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
205-
rewriteRun(pomXml(before, before));
259+
rewriteRun(
260+
pomXml(
261+
"""
262+
<project>
263+
<modelVersion>4.0.0</modelVersion>
264+
<groupId>com.example.jackson</groupId>
265+
<artifactId>test-plugins</artifactId>
266+
<version>1.0.0</version>
267+
<dependencies>
268+
<dependency>
269+
<groupId>org.testcontainers</groupId>
270+
<artifactId>junit-jupiter</artifactId>
271+
<version>1.18.3</version>
272+
<scope>test</scope>
273+
</dependency>
274+
</dependencies>
275+
</project>
276+
"""
277+
)
278+
);
206279
}
207280

208281
@Test
209282
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/477")
210-
void dontExcludeJunit4DependencyfromSpringBootTestcontainers() {
211-
//language=xml
212-
String before = """
213-
<project>
214-
<modelVersion>4.0.0</modelVersion>
215-
<parent>
216-
<groupId>org.springframework.boot</groupId>
217-
<artifactId>spring-boot-starter-parent</artifactId>
218-
<version>3.2.1</version>
219-
<relativePath/> <!-- lookup parent from repository -->
220-
</parent>
221-
<groupId>dev.ted</groupId>
222-
<artifactId>testcontainer-migrate</artifactId>
223-
<version>0.0.1</version>
224-
<dependencies>
225-
<dependency>
226-
<groupId>org.springframework.boot</groupId>
227-
<artifactId>spring-boot-starter</artifactId>
228-
</dependency>
229-
<dependency>
230-
<groupId>org.springframework.boot</groupId>
231-
<artifactId>spring-boot-starter-test</artifactId>
232-
<scope>test</scope>
233-
</dependency>
234-
<dependency>
283+
void dontExcludeJunit4DependencyFromSpringBootTestcontainers() {
284+
rewriteRun(
285+
//language=xml
286+
pomXml(
287+
"""
288+
<project>
289+
<modelVersion>4.0.0</modelVersion>
290+
<parent>
235291
<groupId>org.springframework.boot</groupId>
236-
<artifactId>spring-boot-testcontainers</artifactId>
237-
<scope>test</scope>
238-
</dependency>
239-
<dependency>
240-
<groupId>org.testcontainers</groupId>
241-
<artifactId>junit-jupiter</artifactId>
242-
<scope>test</scope>
243-
</dependency>
244-
</dependencies>
245-
</project>
246-
""";
247-
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
248-
rewriteRun(pomXml(before, before));
292+
<artifactId>spring-boot-starter-parent</artifactId>
293+
<version>3.2.1</version>
294+
<relativePath/> <!-- lookup parent from repository -->
295+
</parent>
296+
<groupId>dev.ted</groupId>
297+
<artifactId>testcontainer-migrate</artifactId>
298+
<version>0.0.1</version>
299+
<dependencies>
300+
<dependency>
301+
<groupId>org.springframework.boot</groupId>
302+
<artifactId>spring-boot-starter</artifactId>
303+
</dependency>
304+
<dependency>
305+
<groupId>org.springframework.boot</groupId>
306+
<artifactId>spring-boot-starter-test</artifactId>
307+
<scope>test</scope>
308+
</dependency>
309+
<dependency>
310+
<groupId>org.springframework.boot</groupId>
311+
<artifactId>spring-boot-testcontainers</artifactId>
312+
<scope>test</scope>
313+
</dependency>
314+
<dependency>
315+
<groupId>org.testcontainers</groupId>
316+
<artifactId>junit-jupiter</artifactId>
317+
<scope>test</scope>
318+
</dependency>
319+
</dependencies>
320+
</project>
321+
"""
322+
)
323+
);
249324
}
250325

251326
// edge case for deprecated use of assertEquals

0 commit comments

Comments
 (0)