Skip to content

Adding dependency guards to the Junit 4 -> 5 migration for when the POM or build.gradle have a dependency on org.testng:testng. #760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
7 changes: 4 additions & 3 deletions src/main/resources/META-INF/rewrite/junit5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ tags:
- testing
- junit
preconditions:
- org.openrewrite.java.search.DoesNotUseType:
fullyQualifiedTypeName: org.testng..*
includeImplicit: true
- org.openrewrite.java.dependencies.search.ModuleHasDependency:
groupIdPattern: org.testng
artifactIdPattern: testng*
invertMarking: true
recipeList:
- org.openrewrite.java.testing.junit5.EnvironmentVariables
- org.openrewrite.java.testing.junit5.UseWiremockExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.java.testing.junit5;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
Expand Down Expand Up @@ -51,11 +52,11 @@ public void defaults(RecipeSpec spec) {
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/145")
@Test
void assertThatReceiver() {
//language=java
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "junit-4", "hamcrest-3")),
//language=java
java(
"""
import org.junit.Assert;
Expand Down Expand Up @@ -129,24 +130,24 @@ void upgradeMavenPluginVersions() {
//language=xml
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.jackson</groupId>
<artifactId>test-plugins</artifactId>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
</plugin>
</plugins>
</build>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.jackson</groupId>
<artifactId>test-plugins</artifactId>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
</plugin>
</plugins>
</build>
</project>
""",
spec -> spec.after(actual -> {
Expand All @@ -161,8 +162,8 @@ void upgradeMavenPluginVersions() {
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
rewriteRun(
//language=xml
pomXml(
"""
<project>
Expand Down Expand Up @@ -229,8 +230,8 @@ void excludeJunit4Dependency() {
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
@Test
void dontExcludeJunit4DependencyFromTestcontainers() {
//language=xml
rewriteRun(
//language=xml
pomXml(
"""
<project>
Expand All @@ -255,8 +256,8 @@ void dontExcludeJunit4DependencyFromTestcontainers() {
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
@Test
void dontExcludeJunit4DependencyFromTestcontainersJupiter() {
//language=xml
rewriteRun(
//language=xml
pomXml(
"""
<project>
Expand Down Expand Up @@ -356,8 +357,8 @@ void test() {
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/443")
@Test
void migrateInheritedTestBeforeAfterAnnotations() {
//language=java
rewriteRun(
//language=java
java(
"""
import org.junit.After;
Expand Down Expand Up @@ -398,6 +399,7 @@ public void test() {
}
"""
),
//language=java
java(
"""
public class A extends AbstractTest {
Expand Down Expand Up @@ -532,8 +534,8 @@ void addMockitoJupiterDependencyIfExtendWithPresent() {
.build()
.activateRecipes("org.openrewrite.java.testing.junit5.UseMockitoExtension")),
mavenProject("sample",
//language=java
srcMainJava(
//language=java
java(
"""
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -611,4 +613,92 @@ public class MyClassTest {
)
);
}

@Nested
class TestngExclude {
@Test
void noChangesIfTestNgGradleDependencyIncluded() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi()),
mavenProject("project",
//language=groovy
buildGradle(
"""
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.testng:testng:7.8.0'
}
tasks.withType(Test).configureEach {
useJUnitPlatform()
}
"""
),
srcTestJava(
//language=java
java(
"""
import org.junit.Ignore;

class ExampleClass {
@Ignore
public void testMethod() {}
}
"""
)
)
)
);
}

@Test
void noChangesIfTestNgMavenDependencyIncluded() {
rewriteRun(
mavenProject("project",
//language=xml
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>dev.ted</groupId>
<artifactId>testcontainer-migrate</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
</dependency>
</dependencies>
</project>
"""
),
srcTestJava(
//language=java
java(
"""
import org.junit.Ignore;

class ExampleClass {
@Ignore
public void testMethod() {}
}
"""
)
)
)
);
}
}
}