diff --git a/build.gradle.kts b/build.gradle.kts index 3c7006b2b..27540a31c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -40,4 +40,9 @@ dependencies { testImplementation("org.openrewrite:rewrite-java-17") testImplementation("org.openrewrite:rewrite-groovy") + + testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release") + +// testImplementation("org.hamcrest:hamcrest:latest.release") +// testImplementation("org.assertj:assertj-core:latest.release") } diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java index 78959c835..04d61dc38 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java @@ -15,6 +15,8 @@ */ package org.openrewrite.java.testing.hamcrest; +import org.intellij.lang.annotations.Language; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -29,10 +31,14 @@ import java.util.stream.Stream; -import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.gradle.Assertions.buildGradle; +import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.java.Assertions.*; +import static org.openrewrite.maven.Assertions.pomXml; @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/212") class MigrateHamcrestToAssertJTest implements RewriteTest { + @Override public void defaults(RecipeSpec spec) { spec @@ -81,14 +87,15 @@ void testEquals() { } """)); } - @DocumentExample + @Test + @DocumentExample void flattenAllOfStringMatchersAndConvert() { rewriteRun( //language=java java(""" import org.junit.jupiter.api.Test; - + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.equalTo; @@ -102,7 +109,7 @@ void test() { assertThat(str1, allOf(equalTo(str2), hasLength(12))); } } - """,""" + """, """ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -426,4 +433,118 @@ void test() { String after = template.formatted(importsAfter, "assertThat(%s).%s(%s);".formatted(actual, assertJAssertion, matcherArgs)); rewriteRun(java(before, after)); } + + @Nested + class Dependencies { + @Language("java") + private static final String JAVA_BEFORE = """ + import org.junit.jupiter.api.Test; + + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.equalTo; + + class ATest { + @Test + void test() { + assertThat("Hello world!", equalTo("Hello world!")); + } + } + """; + @Language("java") + private static final String JAVA_AFTER = """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void test() { + assertThat("Hello world!").isEqualTo("Hello world!"); + } + } + """; + + @Test + void assertjMavenDependencyAddedWithTestScope() { + rewriteRun( + mavenProject("project", + //language=java + srcTestJava(java(JAVA_BEFORE, JAVA_AFTER)), + //language=xml + pomXml(""" + + 4.0.0 + com.example + demo + 0.0.1-SNAPSHOT + + + org.hamcrest + hamcrest + 2.2 + test + + + + """, """ + + 4.0.0 + com.example + demo + 0.0.1-SNAPSHOT + + + org.assertj + assertj-core + 3.24.2 + test + + + org.hamcrest + hamcrest + 2.2 + test + + + + """))); + } + + @Test + void assertjGradleDependencyAddedWithTestScope() { + rewriteRun( + spec -> spec.beforeRecipe(withToolingApi()), + mavenProject("project", + //language=java + srcTestJava(java(JAVA_BEFORE, JAVA_AFTER)), + //language=groovy + buildGradle(""" + plugins { + id "java-library" + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation "org.hamcrest:hamcrest:2.2" + } + """, """ + plugins { + id "java-library" + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation "org.assertj:assertj-core:3.24.2" + testImplementation "org.hamcrest:hamcrest:2.2" + } + """))); + } + } + }