Skip to content

Commit e5ee3de

Browse files
Adding dependency guards to the Junit 4 -> 5 migration for when the POM or build.gradle have a dependency on org.testng:testng. Relies on openrewrite/rewrite#5725
1 parent 5dd6582 commit e5ee3de

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ preconditions:
5757
- org.openrewrite.java.search.DoesNotUseType:
5858
fullyQualifiedTypeName: org.testng..*
5959
includeImplicit: true
60+
- org.openrewrite.gradle.search.DoesNotIncludeDependency:
61+
groupId: org.testng
62+
artifactId: testng
63+
- org.openrewrite.maven.search.DoesNotIncludeDependency:
64+
groupId: org.testng
65+
artifactId: testng
6066
recipeList:
6167
- org.openrewrite.java.testing.junit5.UseWiremockExtension
6268
- org.openrewrite.java.testing.junit5.IgnoreToDisabled

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

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class JUnit5MigrationTest implements RewriteTest {
4040
public void defaults(RecipeSpec spec) {
4141
spec
4242
.parser(JavaParser.fromJavaVersion()
43-
.classpathFromResources(new InMemoryExecutionContext(), "junit-4"))
43+
.classpathFromResources(new InMemoryExecutionContext(), "junit-4", "testng"))
4444
.recipe(Environment.builder()
4545
.scanRuntimeClasspath("org.openrewrite.java.testing.junit5")
4646
.build()
@@ -51,11 +51,11 @@ public void defaults(RecipeSpec spec) {
5151
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/145")
5252
@Test
5353
void assertThatReceiver() {
54-
//language=java
5554
rewriteRun(
5655
spec -> spec
5756
.parser(JavaParser.fromJavaVersion()
5857
.classpathFromResources(new InMemoryExecutionContext(), "junit-4", "hamcrest-3")),
58+
//language=java
5959
java(
6060
"""
6161
import org.junit.Assert;
@@ -477,6 +477,68 @@ void noJunitDependencyIfApiAlreadyPresent() {
477477
);
478478
}
479479

480+
@Test
481+
void noChangesIfTestNgIncluded() {
482+
rewriteRun(
483+
spec -> spec.beforeRecipe(withToolingApi()),
484+
//language=groovy
485+
buildGradle(
486+
"""
487+
plugins {
488+
id 'java-library'
489+
}
490+
repositories {
491+
mavenCentral()
492+
}
493+
dependencies {
494+
testImplementation 'junit:junit:4.12'
495+
testImplementation 'org.testng:testng:7.8.0'
496+
}
497+
tasks.withType(Test).configureEach {
498+
useJUnitPlatform()
499+
}
500+
"""
501+
),
502+
//language=xml
503+
pomXml(
504+
"""
505+
<project>
506+
<modelVersion>4.0.0</modelVersion>
507+
<groupId>dev.ted</groupId>
508+
<artifactId>testcontainer-migrate</artifactId>
509+
<version>0.0.1</version>
510+
<dependencies>
511+
<dependency>
512+
<groupId>junit</groupId>
513+
<artifactId>junit</artifactId>
514+
<version>4.12</version>
515+
<scope>test</scope>
516+
</dependency>
517+
<dependency>
518+
<groupId>org.testng</groupId>
519+
<artifactId>testng</artifactId>
520+
<version>7.8.0</version>
521+
</dependency>
522+
</dependencies>
523+
</project>
524+
"""
525+
),
526+
//language=java
527+
java(
528+
"""
529+
import org.junit.Ignore;
530+
import org.testng.annotations.Test;
531+
532+
class ExampleClass {
533+
@Ignore
534+
@Test
535+
public void testMethod() {}
536+
}
537+
"""
538+
)
539+
);
540+
}
541+
480542
@Test
481543
void bumpSurefireOnOlderMavenVersions() {
482544
rewriteRun(

0 commit comments

Comments
 (0)