Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/main/resources/META-INF/rewrite/junit5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.ExcludeJUnit4UnlessUsingTestcontainers
- org.openrewrite.java.dependencies.RemoveDependency:
groupId: org.junit.vintage
artifactId: junit-vintage-engine
Expand Down Expand Up @@ -145,7 +131,19 @@ 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.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
artifactId: '*'
recipeList:
- org.openrewrite.maven.ExcludeDependency:
groupId: junit
artifactId: junit
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.junit5.UseHamcrestAssertThat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,95 +157,170 @@ void upgradeMavenPluginVersions() {
);
}

@Test
void excludeJunit4Dependency() {
// Just using play-test_2.13 as an example because it appears to still depend on junit.
Copy link
Contributor Author

@DidierLoiseau DidierLoiseau Jun 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually wanted to provide a pom with the junit dependency in the test, and then run it as:

        rewriteRun(
          mavenProject("depends-on-junit", pomXml(remoteDepWithJunit, SourceSpec::skip)),
          mavenProject("needs-exclusion", pomXml(before, after)));

and somehow it works – the exclusion is added as intended – but it also gets a MavenDownloadException added in the output.

I was trying to do the same as other Maven tests that use SourceSpec::skip, but they all seem to do that for a parent, not a dependency.

// In practice, this would probably just break it, I assume.
//language=xml
rewriteRun(
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>dev.ted</groupId>
<artifactId>needs-exclusion</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play-test_2.13</artifactId>
<version>2.9.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>dev.ted</groupId>
<artifactId>needs-exclusion</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play-test_2.13</artifactId>
<version>2.9.6</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
void dontExcludeJunit4DependencyfromTestcontainers() {
void dontExcludeJunit4DependencyFromTestcontainers() {
//language=xml
String before = """
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.jackson</groupId>
<artifactId>test-plugins</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
""";
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
rewriteRun(pomXml(before, before));
rewriteRun(
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.jackson</groupId>
<artifactId>test-plugins</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
void dontExcludeJunit4DependencyfromTestcontainersJupiter() {
void dontExcludeJunit4DependencyFromTestcontainersJupiter() {
//language=xml
String before = """
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.jackson</groupId>
<artifactId>test-plugins</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
""";
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
rewriteRun(pomXml(before, before));
rewriteRun(
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.jackson</groupId>
<artifactId>test-plugins</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/477")
void dontExcludeJunit4DependencyfromSpringBootTestcontainers() {
//language=xml
String before = """
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>dev.ted</groupId>
<artifactId>testcontainer-migrate</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
void dontExcludeJunit4DependencyFromSpringBootTestcontainers() {
rewriteRun(
//language=xml
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
""";
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
rewriteRun(pomXml(before, before));
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>dev.ted</groupId>
<artifactId>testcontainer-migrate</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
"""
)
);
}

// edge case for deprecated use of assertEquals
Expand Down