Skip to content

Commit 595a273

Browse files
simonzntimtebeek
andauthored
Fixes broken testcontainer dependencies after JUnit4to5Migration (#430)
* Adds test case for broken testcontainers dependency Reproduces #429 * Remove JUnit4 exclusion from testcontainers dependency Fixes #429 * Document why output is identical but still different --------- Co-authored-by: e512271 <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent f30412a commit 595a273

File tree

2 files changed

+93
-62
lines changed

2 files changed

+93
-62
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ recipeList:
9595
- org.openrewrite.maven.ExcludeDependency:
9696
groupId: junit
9797
artifactId: junit
98+
# Workaround for https://github.com/testcontainers/testcontainers-java/issues/970:
99+
- org.openrewrite.maven.RemoveExclusion:
100+
groupId: org.testcontainers
101+
artifactId: testcontainers
102+
exclusionGroupId: junit
103+
exclusionArtifactId: junit
98104
- org.openrewrite.java.dependencies.RemoveDependency:
99105
groupId: org.junit.vintage
100106
artifactId: junit-vintage-engine

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

Lines changed: 87 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static org.openrewrite.maven.Assertions.pomXml;
2828

2929
class JUnit5MigrationTest implements RewriteTest {
30-
3130
@Override
3231
public void defaults(RecipeSpec spec) {
3332
spec
@@ -43,25 +42,26 @@ public void defaults(RecipeSpec spec) {
4342
@Test
4443
void classReference() {
4544
rewriteRun(
45+
//language=java
4646
java(
4747
"""
48-
import org.junit.Test;
49-
50-
public class Sample {
51-
void method() {
52-
Class<Test> c = Test.class;
53-
}
54-
}
55-
""",
56-
"""
57-
import org.junit.jupiter.api.Test;
58-
59-
public class Sample {
60-
void method() {
61-
Class<Test> c = Test.class;
62-
}
63-
}
48+
import org.junit.Test;
49+
50+
public class Sample {
51+
void method() {
52+
Class<Test> c = Test.class;
53+
}
54+
}
55+
""",
6456
"""
57+
import org.junit.jupiter.api.Test;
58+
59+
public class Sample {
60+
void method() {
61+
Class<Test> c = Test.class;
62+
}
63+
}
64+
"""
6565
)
6666
);
6767
}
@@ -112,66 +112,91 @@ void filterShouldRemoveUnusedConfig() {
112112
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/279")
113113
void upgradeMavenPluginVersions() {
114114
rewriteRun(
115+
//language=xml
115116
pomXml(
116117
"""
117-
<project>
118-
<modelVersion>4.0.0</modelVersion>
119-
<groupId>com.example.jackson</groupId>
120-
<artifactId>test-plugins</artifactId>
121-
<version>1.0.0</version>
122-
<build>
123-
<plugins>
124-
<plugin>
125-
<groupId>org.apache.maven.plugins</groupId>
126-
<artifactId>maven-surefire-plugin</artifactId>
127-
<version>2.20.1</version>
128-
</plugin>
129-
<plugin>
130-
<groupId>org.apache.maven.plugins</groupId>
131-
<artifactId>maven-failsafe-plugin</artifactId>
132-
<version>2.20.1</version>
133-
</plugin>
134-
</plugins>
135-
</build>
136-
</project>
137-
""",
138-
"""
139-
<project>
140-
<modelVersion>4.0.0</modelVersion>
141-
<groupId>com.example.jackson</groupId>
142-
<artifactId>test-plugins</artifactId>
143-
<version>1.0.0</version>
144-
<build>
145-
<plugins>
146-
<plugin>
147-
<groupId>org.apache.maven.plugins</groupId>
148-
<artifactId>maven-surefire-plugin</artifactId>
149-
<version>2.22.2</version>
150-
</plugin>
151-
<plugin>
152-
<groupId>org.apache.maven.plugins</groupId>
153-
<artifactId>maven-failsafe-plugin</artifactId>
154-
<version>2.22.2</version>
155-
</plugin>
156-
</plugins>
157-
</build>
158-
</project>
118+
<project>
119+
<modelVersion>4.0.0</modelVersion>
120+
<groupId>com.example.jackson</groupId>
121+
<artifactId>test-plugins</artifactId>
122+
<version>1.0.0</version>
123+
<build>
124+
<plugins>
125+
<plugin>
126+
<groupId>org.apache.maven.plugins</groupId>
127+
<artifactId>maven-surefire-plugin</artifactId>
128+
<version>2.20.1</version>
129+
</plugin>
130+
<plugin>
131+
<groupId>org.apache.maven.plugins</groupId>
132+
<artifactId>maven-failsafe-plugin</artifactId>
133+
<version>2.20.1</version>
134+
</plugin>
135+
</plugins>
136+
</build>
137+
</project>
138+
""",
159139
"""
140+
<project>
141+
<modelVersion>4.0.0</modelVersion>
142+
<groupId>com.example.jackson</groupId>
143+
<artifactId>test-plugins</artifactId>
144+
<version>1.0.0</version>
145+
<build>
146+
<plugins>
147+
<plugin>
148+
<groupId>org.apache.maven.plugins</groupId>
149+
<artifactId>maven-surefire-plugin</artifactId>
150+
<version>2.22.2</version>
151+
</plugin>
152+
<plugin>
153+
<groupId>org.apache.maven.plugins</groupId>
154+
<artifactId>maven-failsafe-plugin</artifactId>
155+
<version>2.22.2</version>
156+
</plugin>
157+
</plugins>
158+
</build>
159+
</project>
160+
"""
160161
)
161162
);
162163
}
163164

165+
@Test
166+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
167+
void dontExcludeJunit4DependencyfromTestcontainers() {
168+
//language=xml
169+
String before = """
170+
<project>
171+
<modelVersion>4.0.0</modelVersion>
172+
<groupId>com.example.jackson</groupId>
173+
<artifactId>test-plugins</artifactId>
174+
<version>1.0.0</version>
175+
<dependencies>
176+
<dependency>
177+
<groupId>org.testcontainers</groupId>
178+
<artifactId>testcontainers</artifactId>
179+
<version>1.18.3</version>
180+
<scope>test</scope>
181+
</dependency>
182+
</dependencies>
183+
</project>
184+
""";
185+
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
186+
rewriteRun(pomXml(before, before));
187+
}
188+
164189
// edge case for deprecated use of assertEquals
165190
// https://junit.org/junit4/javadoc/4.13/org/junit/Assert.html#assertEquals(java.lang.Object%5B%5D,%20java.lang.Object%5B%5D)
166191
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/pull/384")
167192
@Test
168193
void assertEqualsWithArrayArgumentToAssertArrayEquals() {
169-
//language=java
170194
rewriteRun(
195+
//language=java
171196
java(
172197
"""
173198
import org.junit.Assert;
174-
199+
175200
class MyTest {
176201
void test() {
177202
Assert.assertEquals(new Object[1], new Object[1]);
@@ -180,7 +205,7 @@ void test() {
180205
""",
181206
"""
182207
import org.junit.jupiter.api.Assertions;
183-
208+
184209
class MyTest {
185210
void test() {
186211
Assertions.assertArrayEquals(new Object[1], new Object[1]);

0 commit comments

Comments
 (0)