From 6f91c2f79312de3ac52a734cdb15b39702c7fc73 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 13 Jun 2024 13:00:39 +0200 Subject: [PATCH] Migrate all Hamcrest matchers from core package Fixes #519 --- .../resources/META-INF/rewrite/hamcrest.yml | 2 +- .../MigrateHamcrestToAssertJTest.java | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main/resources/META-INF/rewrite/hamcrest.yml b/src/main/resources/META-INF/rewrite/hamcrest.yml index 76dd930f4..7439f28ce 100644 --- a/src/main/resources/META-INF/rewrite/hamcrest.yml +++ b/src/main/resources/META-INF/rewrite/hamcrest.yml @@ -41,7 +41,7 @@ tags: recipeList: # First change `is(..)` to `Matchers.is(..)` for consistent matching - org.openrewrite.java.ChangeMethodTargetToStatic: - methodPattern: org.hamcrest.core.Is is(..) + methodPattern: org.hamcrest.core.* *(..) fullyQualifiedTargetTypeName: org.hamcrest.Matchers # Then remove wrapping `is(Matcher)` calls such that further recipes will match 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 685f993df..64ad82036 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java @@ -719,4 +719,45 @@ void ba() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/519") + void isEqualMatcherFromCore() { + rewriteRun( + //language=java + java( + """ + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.core.IsEqual.equalTo; + import static org.hamcrest.core.IsNot.not; + import static org.hamcrest.core.IsSame.sameInstance; + + import org.junit.jupiter.api.Test; + + class DebugTest { + @Test + void ba() { + assertThat(System.out, equalTo(System.out)); + assertThat(System.out, not(System.out)); + assertThat(System.out, sameInstance(System.out)); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + + import org.junit.jupiter.api.Test; + + class DebugTest { + @Test + void ba() { + assertThat(System.out).isEqualTo(System.out); + assertThat(System.out).isNotEqualTo(System.out); + assertThat(System.out).isSameAs(System.out); + } + } + """ + ) + ); + } }