From 862b3aa051f89c4d25e63768f0cd28033d2ad5de Mon Sep 17 00:00:00 2001 From: Didier Loiseau Date: Sat, 7 Jun 2025 11:45:04 +0200 Subject: [PATCH 1/2] Do not ever exclude junit if Testcontainers is used --- .../resources/META-INF/rewrite/junit5.yml | 28 +++---- .../testing/junit5/JUnit5MigrationTest.java | 79 +++++++++++++++++-- 2 files changed, 85 insertions(+), 22 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/junit5.yml b/src/main/resources/META-INF/rewrite/junit5.yml index e54d2acfa..c5ea858fb 100755 --- a/src/main/resources/META-INF/rewrite/junit5.yml +++ b/src/main/resources/META-INF/rewrite/junit5.yml @@ -102,21 +102,7 @@ recipeList: - org.openrewrite.java.dependencies.RemoveDependency: groupId: junit artifactId: junit - - org.openrewrite.maven.ExcludeDependency: - groupId: junit - artifactId: junit - # Workaround for https://github.com/testcontainers/testcontainers-java/issues/970: - - org.openrewrite.maven.RemoveExclusion: - groupId: org.testcontainers - artifactId: '*' - exclusionGroupId: junit - exclusionArtifactId: junit - # Similar for https://github.com/openrewrite/rewrite-testing-frameworks/issues/477 - - org.openrewrite.maven.RemoveExclusion: - groupId: org.springframework.boot - artifactId: spring-boot-testcontainers - exclusionGroupId: junit - exclusionArtifactId: junit + - org.openrewrite.java.testing.junit5.ExcludeJUnit4IfNoTestcontainers - org.openrewrite.java.dependencies.RemoveDependency: groupId: org.junit.vintage artifactId: junit-vintage-engine @@ -145,7 +131,17 @@ recipeList: newFullyQualifiedTypeName: org.jbehave.core.junit.JupiterStories - org.openrewrite.java.testing.arquillian.ArquillianJUnit4ToArquillianJUnit5 - org.openrewrite.java.testing.dbrider.MigrateDbRiderSpringToDbRiderJUnit5 - +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.testing.junit5.ExcludeJUnit4IfNoTestcontainers +preconditions: + - org.openrewrite.maven.search.DoesNotIncludeDependency: + groupId: org.testcontainers + artifactId: '*' +recipeList: + - org.openrewrite.maven.ExcludeDependency: + groupId: junit + artifactId: junit --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.testing.junit5.UseHamcrestAssertThat diff --git a/src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java b/src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java index 586d9352b..6e5be4b00 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java @@ -157,9 +157,76 @@ void upgradeMavenPluginVersions() { ); } + @Test + void excludeJunit4Dependency() { + // Just using play-test_2.13 as an example because it appears to still depend on junit. + // In practice, this would probably just break it, I assume. + //language=xml + String before = """ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.2.1 + + + dev.ted + needs-exclusion + 0.0.1 + + + org.springframework.boot + spring-boot-starter + + + com.typesafe.play + play-test_2.13 + 2.9.6 + test + + + + """; + //language=xml + String after = """ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.2.1 + + + dev.ted + needs-exclusion + 0.0.1 + + + org.springframework.boot + spring-boot-starter + + + com.typesafe.play + play-test_2.13 + 2.9.6 + test + + + junit + junit + + + + + + """; + rewriteRun(pomXml(before, after)); + } + @Test @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429") - void dontExcludeJunit4DependencyfromTestcontainers() { + void dontExcludeJunit4DependencyFromTestcontainers() { //language=xml String before = """ @@ -178,12 +245,12 @@ void dontExcludeJunit4DependencyfromTestcontainers() { """; // Output identical, but we want to make sure we don't exclude junit4 from testcontainers - rewriteRun(pomXml(before, before)); + rewriteRun(pomXml(before)); } @Test @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429") - void dontExcludeJunit4DependencyfromTestcontainersJupiter() { + void dontExcludeJunit4DependencyFromTestcontainersJupiter() { //language=xml String before = """ @@ -202,12 +269,12 @@ void dontExcludeJunit4DependencyfromTestcontainersJupiter() { """; // Output identical, but we want to make sure we don't exclude junit4 from testcontainers - rewriteRun(pomXml(before, before)); + rewriteRun(pomXml(before)); } @Test @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/477") - void dontExcludeJunit4DependencyfromSpringBootTestcontainers() { + void dontExcludeJunit4DependencyFromSpringBootTestcontainers() { //language=xml String before = """ @@ -245,7 +312,7 @@ void dontExcludeJunit4DependencyfromSpringBootTestcontainers() { """; // Output identical, but we want to make sure we don't exclude junit4 from testcontainers - rewriteRun(pomXml(before, before)); + rewriteRun(pomXml(before)); } // edge case for deprecated use of assertEquals From 798fbbaf7c5eb7a498fc01e9e3fd60504a3f70ae Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 7 Jun 2025 14:17:43 +0200 Subject: [PATCH 2/2] Slight polish to remove left over comments --- .../resources/META-INF/rewrite/junit5.yml | 6 +- .../testing/junit5/JUnit5MigrationTest.java | 270 +++++++++--------- 2 files changed, 143 insertions(+), 133 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/junit5.yml b/src/main/resources/META-INF/rewrite/junit5.yml index c5ea858fb..2ed727614 100755 --- a/src/main/resources/META-INF/rewrite/junit5.yml +++ b/src/main/resources/META-INF/rewrite/junit5.yml @@ -102,7 +102,7 @@ recipeList: - org.openrewrite.java.dependencies.RemoveDependency: groupId: junit artifactId: junit - - org.openrewrite.java.testing.junit5.ExcludeJUnit4IfNoTestcontainers + - org.openrewrite.java.testing.junit5.ExcludeJUnit4UnlessUsingTestcontainers - org.openrewrite.java.dependencies.RemoveDependency: groupId: org.junit.vintage artifactId: junit-vintage-engine @@ -133,7 +133,9 @@ recipeList: - org.openrewrite.java.testing.dbrider.MigrateDbRiderSpringToDbRiderJUnit5 --- type: specs.openrewrite.org/v1beta/recipe -name: org.openrewrite.java.testing.junit5.ExcludeJUnit4IfNoTestcontainers +name: org.openrewrite.java.testing.junit5.ExcludeJUnit4UnlessUsingTestcontainers +displayName: Exclude JUnit 4, unless Testcontainers is used +description: Excludes JUnit 4, as it ought not to be necessary in a JUnit 5 project, unless Testcontainers is used. preconditions: - org.openrewrite.maven.search.DoesNotIncludeDependency: groupId: org.testcontainers diff --git a/src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java b/src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java index 6e5be4b00..aed1616df 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java @@ -162,157 +162,165 @@ void excludeJunit4Dependency() { // Just using play-test_2.13 as an example because it appears to still depend on junit. // In practice, this would probably just break it, I assume. //language=xml - String before = """ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.2.1 - - - dev.ted - needs-exclusion - 0.0.1 - - + rewriteRun( + pomXml( + """ + + 4.0.0 + org.springframework.boot - spring-boot-starter - - - com.typesafe.play - play-test_2.13 - 2.9.6 - test - - - - """; - //language=xml - String after = """ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.2.1 - - - dev.ted - needs-exclusion - 0.0.1 - - + spring-boot-starter-parent + 3.2.1 + + + dev.ted + needs-exclusion + 0.0.1 + + + org.springframework.boot + spring-boot-starter + + + com.typesafe.play + play-test_2.13 + 2.9.6 + test + + + + """, + """ + + 4.0.0 + org.springframework.boot - spring-boot-starter - - - com.typesafe.play - play-test_2.13 - 2.9.6 - test - - - junit - junit - - - - - - """; - rewriteRun(pomXml(before, after)); + spring-boot-starter-parent + 3.2.1 + + + dev.ted + needs-exclusion + 0.0.1 + + + org.springframework.boot + spring-boot-starter + + + com.typesafe.play + play-test_2.13 + 2.9.6 + test + + + junit + junit + + + + + + """ + ) + ); } @Test @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429") void dontExcludeJunit4DependencyFromTestcontainers() { //language=xml - String before = """ - - 4.0.0 - com.example.jackson - test-plugins - 1.0.0 - - - org.testcontainers - testcontainers - 1.18.3 - test - - - - """; - // Output identical, but we want to make sure we don't exclude junit4 from testcontainers - rewriteRun(pomXml(before)); + rewriteRun( + pomXml( + """ + + 4.0.0 + com.example.jackson + test-plugins + 1.0.0 + + + org.testcontainers + testcontainers + 1.18.3 + test + + + + """ + ) + ); } @Test @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429") void dontExcludeJunit4DependencyFromTestcontainersJupiter() { //language=xml - String before = """ - - 4.0.0 - com.example.jackson - test-plugins - 1.0.0 - - - org.testcontainers - junit-jupiter - 1.18.3 - test - - - - """; - // Output identical, but we want to make sure we don't exclude junit4 from testcontainers - rewriteRun(pomXml(before)); + rewriteRun( + pomXml( + """ + + 4.0.0 + com.example.jackson + test-plugins + 1.0.0 + + + org.testcontainers + junit-jupiter + 1.18.3 + test + + + + """ + ) + ); } @Test @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/477") void dontExcludeJunit4DependencyFromSpringBootTestcontainers() { - //language=xml - String before = """ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.2.1 - - - dev.ted - testcontainer-migrate - 0.0.1 - - - org.springframework.boot - spring-boot-starter - - - org.springframework.boot - spring-boot-starter-test - test - - + rewriteRun( + //language=xml + pomXml( + """ + + 4.0.0 + org.springframework.boot - spring-boot-testcontainers - test - - - org.testcontainers - junit-jupiter - test - - - - """; - // Output identical, but we want to make sure we don't exclude junit4 from testcontainers - rewriteRun(pomXml(before)); + spring-boot-starter-parent + 3.2.1 + + + dev.ted + testcontainer-migrate + 0.0.1 + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-testcontainers + test + + + org.testcontainers + junit-jupiter + test + + + + """ + ) + ); } // edge case for deprecated use of assertEquals