Skip to content

Commit 9a2082a

Browse files
steve-aom-elliotttimtebeekgithub-actions[bot]
authored
Adding dependency guards to the Junit 4 -> 5 migration for when the POM or build.gradle have a dependency on org.testng:testng. (#760)
* 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 * Updating `DoesNotIncludeDependency` recipe call to be the shared one in `rewrite-java-dependencies` * Avoid tests passing for the wrong reason (class present) * Only pass testng dependency into the single test that needs it * Match any testng dependency * Adding TestNgGuard scanning recipe. Needs refinement, but this is a first pass * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Making separate `TestNgGuard` tests and cleaning up other code * Cleanup of import Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Made the `TestNgGuard` more flexible for situations where there isn't an enclosing project for a source file, which was a majority of our existing tests. - If it figures out it's a loose file not in a `JavaProject`, it can't ensure project scope anyway, so falls back to solely whether the file itself has a TestNG dependency or uses TestNG types instead for those * Do not use getters internally * Adopt `ModuleHasDependency` and drop odd test: only look at dependencies Assuming classes can not be present without those dependencies * Revert change to `TestNgToAssertJTest` * Drop tests that were showing improbable use of types * Switching to using the new `invertMarking` option on `ModuleHasDependency` for the TestNG guard for JUnit --------- Co-authored-by: Tim te Beek <tim@moderne.io> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 74366cf commit 9a2082a

File tree

2 files changed

+118
-27
lines changed

2 files changed

+118
-27
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ tags:
5858
- testing
5959
- junit
6060
preconditions:
61-
- org.openrewrite.java.search.DoesNotUseType:
62-
fullyQualifiedTypeName: org.testng..*
63-
includeImplicit: true
61+
- org.openrewrite.java.dependencies.search.ModuleHasDependency:
62+
groupIdPattern: org.testng
63+
artifactIdPattern: testng*
64+
invertMarking: true
6465
recipeList:
6566
- org.openrewrite.java.testing.junit5.EnvironmentVariables
6667
- org.openrewrite.java.testing.junit5.UseWiremockExtension

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

Lines changed: 114 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.testing.junit5;
1717

18+
import org.junit.jupiter.api.Nested;
1819
import org.junit.jupiter.api.Test;
1920
import org.openrewrite.DocumentExample;
2021
import org.openrewrite.InMemoryExecutionContext;
@@ -51,11 +52,11 @@ public void defaults(RecipeSpec spec) {
5152
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/145")
5253
@Test
5354
void assertThatReceiver() {
54-
//language=java
5555
rewriteRun(
5656
spec -> spec
5757
.parser(JavaParser.fromJavaVersion()
5858
.classpathFromResources(new InMemoryExecutionContext(), "junit-4", "hamcrest-3")),
59+
//language=java
5960
java(
6061
"""
6162
import org.junit.Assert;
@@ -129,24 +130,24 @@ void upgradeMavenPluginVersions() {
129130
//language=xml
130131
"""
131132
<project>
132-
<modelVersion>4.0.0</modelVersion>
133-
<groupId>com.example.jackson</groupId>
134-
<artifactId>test-plugins</artifactId>
135-
<version>1.0.0</version>
136-
<build>
137-
<plugins>
138-
<plugin>
139-
<groupId>org.apache.maven.plugins</groupId>
140-
<artifactId>maven-surefire-plugin</artifactId>
141-
<version>2.20.1</version>
142-
</plugin>
143-
<plugin>
144-
<groupId>org.apache.maven.plugins</groupId>
145-
<artifactId>maven-failsafe-plugin</artifactId>
146-
<version>2.20.1</version>
147-
</plugin>
148-
</plugins>
149-
</build>
133+
<modelVersion>4.0.0</modelVersion>
134+
<groupId>com.example.jackson</groupId>
135+
<artifactId>test-plugins</artifactId>
136+
<version>1.0.0</version>
137+
<build>
138+
<plugins>
139+
<plugin>
140+
<groupId>org.apache.maven.plugins</groupId>
141+
<artifactId>maven-surefire-plugin</artifactId>
142+
<version>2.20.1</version>
143+
</plugin>
144+
<plugin>
145+
<groupId>org.apache.maven.plugins</groupId>
146+
<artifactId>maven-failsafe-plugin</artifactId>
147+
<version>2.20.1</version>
148+
</plugin>
149+
</plugins>
150+
</build>
150151
</project>
151152
""",
152153
spec -> spec.after(actual -> {
@@ -161,8 +162,8 @@ void upgradeMavenPluginVersions() {
161162
void excludeJunit4Dependency() {
162163
// Just using play-test_2.13 as an example because it appears to still depend on junit.
163164
// In practice, this would probably just break it, I assume.
164-
//language=xml
165165
rewriteRun(
166+
//language=xml
166167
pomXml(
167168
"""
168169
<project>
@@ -229,8 +230,8 @@ void excludeJunit4Dependency() {
229230
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
230231
@Test
231232
void dontExcludeJunit4DependencyFromTestcontainers() {
232-
//language=xml
233233
rewriteRun(
234+
//language=xml
234235
pomXml(
235236
"""
236237
<project>
@@ -255,8 +256,8 @@ void dontExcludeJunit4DependencyFromTestcontainers() {
255256
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
256257
@Test
257258
void dontExcludeJunit4DependencyFromTestcontainersJupiter() {
258-
//language=xml
259259
rewriteRun(
260+
//language=xml
260261
pomXml(
261262
"""
262263
<project>
@@ -356,8 +357,8 @@ void test() {
356357
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/443")
357358
@Test
358359
void migrateInheritedTestBeforeAfterAnnotations() {
359-
//language=java
360360
rewriteRun(
361+
//language=java
361362
java(
362363
"""
363364
import org.junit.After;
@@ -398,6 +399,7 @@ public void test() {
398399
}
399400
"""
400401
),
402+
//language=java
401403
java(
402404
"""
403405
public class A extends AbstractTest {
@@ -532,8 +534,8 @@ void addMockitoJupiterDependencyIfExtendWithPresent() {
532534
.build()
533535
.activateRecipes("org.openrewrite.java.testing.junit5.UseMockitoExtension")),
534536
mavenProject("sample",
535-
//language=java
536537
srcMainJava(
538+
//language=java
537539
java(
538540
"""
539541
import org.junit.runner.RunWith;
@@ -611,4 +613,92 @@ public class MyClassTest {
611613
)
612614
);
613615
}
616+
617+
@Nested
618+
class TestngExclude {
619+
@Test
620+
void noChangesIfTestNgGradleDependencyIncluded() {
621+
rewriteRun(
622+
spec -> spec.beforeRecipe(withToolingApi()),
623+
mavenProject("project",
624+
//language=groovy
625+
buildGradle(
626+
"""
627+
plugins {
628+
id 'java-library'
629+
}
630+
repositories {
631+
mavenCentral()
632+
}
633+
dependencies {
634+
testImplementation 'junit:junit:4.12'
635+
testImplementation 'org.testng:testng:7.8.0'
636+
}
637+
tasks.withType(Test).configureEach {
638+
useJUnitPlatform()
639+
}
640+
"""
641+
),
642+
srcTestJava(
643+
//language=java
644+
java(
645+
"""
646+
import org.junit.Ignore;
647+
648+
class ExampleClass {
649+
@Ignore
650+
public void testMethod() {}
651+
}
652+
"""
653+
)
654+
)
655+
)
656+
);
657+
}
658+
659+
@Test
660+
void noChangesIfTestNgMavenDependencyIncluded() {
661+
rewriteRun(
662+
mavenProject("project",
663+
//language=xml
664+
pomXml(
665+
"""
666+
<project>
667+
<modelVersion>4.0.0</modelVersion>
668+
<groupId>dev.ted</groupId>
669+
<artifactId>testcontainer-migrate</artifactId>
670+
<version>0.0.1</version>
671+
<dependencies>
672+
<dependency>
673+
<groupId>junit</groupId>
674+
<artifactId>junit</artifactId>
675+
<version>4.12</version>
676+
<scope>test</scope>
677+
</dependency>
678+
<dependency>
679+
<groupId>org.testng</groupId>
680+
<artifactId>testng</artifactId>
681+
<version>7.8.0</version>
682+
</dependency>
683+
</dependencies>
684+
</project>
685+
"""
686+
),
687+
srcTestJava(
688+
//language=java
689+
java(
690+
"""
691+
import org.junit.Ignore;
692+
693+
class ExampleClass {
694+
@Ignore
695+
public void testMethod() {}
696+
}
697+
"""
698+
)
699+
)
700+
)
701+
);
702+
}
703+
}
614704
}

0 commit comments

Comments
 (0)