1515 */
1616package org .openrewrite .java .testing .hamcrest ;
1717
18+ import org .intellij .lang .annotations .Language ;
19+ import org .junit .jupiter .api .Nested ;
1820import org .junit .jupiter .api .Test ;
1921import org .junit .jupiter .params .ParameterizedTest ;
2022import org .junit .jupiter .params .provider .Arguments ;
2931
3032import java .util .stream .Stream ;
3133
32- import static org .openrewrite .java .Assertions .java ;
34+ import static org .openrewrite .gradle .Assertions .buildGradle ;
35+ import static org .openrewrite .gradle .Assertions .withToolingApi ;
36+ import static org .openrewrite .java .Assertions .*;
37+ import static org .openrewrite .maven .Assertions .pomXml ;
3338
3439@ Issue ("https://github.com/openrewrite/rewrite-testing-frameworks/issues/212" )
3540class MigrateHamcrestToAssertJTest implements RewriteTest {
41+
3642 @ Override
3743 public void defaults (RecipeSpec spec ) {
3844 spec
@@ -81,14 +87,15 @@ void testEquals() {
8187 }
8288 """ ));
8389 }
84- @ DocumentExample
90+
8591 @ Test
92+ @ DocumentExample
8693 void flattenAllOfStringMatchersAndConvert () {
8794 rewriteRun (
8895 //language=java
8996 java ("""
9097 import org.junit.jupiter.api.Test;
91-
98+
9299 import static org.hamcrest.MatcherAssert.assertThat;
93100 import static org.hamcrest.Matchers.allOf;
94101 import static org.hamcrest.Matchers.equalTo;
@@ -102,7 +109,7 @@ void test() {
102109 assertThat(str1, allOf(equalTo(str2), hasLength(12)));
103110 }
104111 }
105- """ ,"""
112+ """ , """
106113 import org.junit.jupiter.api.Test;
107114
108115 import static org.assertj.core.api.Assertions.assertThat;
@@ -426,4 +433,118 @@ void test() {
426433 String after = template .formatted (importsAfter , "assertThat(%s).%s(%s);" .formatted (actual , assertJAssertion , matcherArgs ));
427434 rewriteRun (java (before , after ));
428435 }
436+
437+ @ Nested
438+ class Dependencies {
439+ @ Language ("java" )
440+ private static final String JAVA_BEFORE = """
441+ import org.junit.jupiter.api.Test;
442+
443+ import static org.hamcrest.MatcherAssert.assertThat;
444+ import static org.hamcrest.Matchers.equalTo;
445+
446+ class ATest {
447+ @Test
448+ void test() {
449+ assertThat("Hello world!", equalTo("Hello world!"));
450+ }
451+ }
452+ """ ;
453+ @ Language ("java" )
454+ private static final String JAVA_AFTER = """
455+ import org.junit.jupiter.api.Test;
456+
457+ import static org.assertj.core.api.Assertions.assertThat;
458+
459+ class ATest {
460+ @Test
461+ void test() {
462+ assertThat("Hello world!").isEqualTo("Hello world!");
463+ }
464+ }
465+ """ ;
466+
467+ @ Test
468+ void assertjMavenDependencyAddedWithTestScope () {
469+ rewriteRun (
470+ mavenProject ("project" ,
471+ //language=java
472+ srcTestJava (java (JAVA_BEFORE , JAVA_AFTER )),
473+ //language=xml
474+ pomXml ("""
475+ <project>
476+ <modelVersion>4.0.0</modelVersion>
477+ <groupId>com.example</groupId>
478+ <artifactId>demo</artifactId>
479+ <version>0.0.1-SNAPSHOT</version>
480+ <dependencies>
481+ <dependency>
482+ <groupId>org.hamcrest</groupId>
483+ <artifactId>hamcrest</artifactId>
484+ <version>2.2</version>
485+ <scope>test</scope>
486+ </dependency>
487+ </dependencies>
488+ </project>
489+ """ , """
490+ <project>
491+ <modelVersion>4.0.0</modelVersion>
492+ <groupId>com.example</groupId>
493+ <artifactId>demo</artifactId>
494+ <version>0.0.1-SNAPSHOT</version>
495+ <dependencies>
496+ <dependency>
497+ <groupId>org.assertj</groupId>
498+ <artifactId>assertj-core</artifactId>
499+ <version>3.24.2</version>
500+ <scope>test</scope>
501+ </dependency>
502+ <dependency>
503+ <groupId>org.hamcrest</groupId>
504+ <artifactId>hamcrest</artifactId>
505+ <version>2.2</version>
506+ <scope>test</scope>
507+ </dependency>
508+ </dependencies>
509+ </project>
510+ """ )));
511+ }
512+
513+ @ Test
514+ void assertjGradleDependencyAddedWithTestScope () {
515+ rewriteRun (
516+ spec -> spec .beforeRecipe (withToolingApi ()),
517+ mavenProject ("project" ,
518+ //language=java
519+ srcTestJava (java (JAVA_BEFORE , JAVA_AFTER )),
520+ //language=groovy
521+ buildGradle ("""
522+ plugins {
523+ id "java-library"
524+ }
525+
526+ repositories {
527+ mavenCentral()
528+ }
529+
530+ dependencies {
531+ testImplementation "org.hamcrest:hamcrest:2.2"
532+ }
533+ """ , """
534+ plugins {
535+ id "java-library"
536+ }
537+
538+ repositories {
539+ mavenCentral()
540+ }
541+
542+ dependencies {
543+ testImplementation "org.assertj:assertj-core:3.24.2"
544+ testImplementation "org.hamcrest:hamcrest:2.2"
545+ }
546+ """ )));
547+ }
548+ }
549+
429550}
0 commit comments