Skip to content

Commit 959c88b

Browse files
committed
Fix order of Hamcrest migration recipes
Make sure to run `HamcrestOfMatchersToAssertJ` before `HamcrestIsMatcherToAssertJ`, so that `assertThat(x, allOf(..))` is first converted to `assertThat(x).satisfies(..)`, where any previous `is(..)` assertion will now be expanded to a `arg -> assertThat(arg, is(..))` lambda, which can then be handled by `HamcrestIsMatcherToAssertJ`. See: - #858
1 parent ebb0358 commit 959c88b

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,16 @@ recipeList:
9393
onlyIfUsing: org.hamcrest.*
9494
acceptTransitive: true
9595

96-
# First change `is(..)` to `Matchers.is(..)` for consistent matching
96+
# Normalize Hamcrest imports to `org.hamcrest.Matchers` and remove wrapping `is(Matcher)` calls
9797
- org.openrewrite.java.testing.hamcrest.ConsistentHamcrestMatcherImports
9898

99-
# Then remove calls to `MatcherAssert.assertThat(String, is(Matcher))`
100-
- org.openrewrite.java.testing.hamcrest.HamcrestIsMatcherToAssertJ
101-
102-
# Then remove calls to `MatcherAssert.assertThat(String, anyOf(..))` and `allOf(..)`
99+
# Convert `assertThat(x, allOf(..))` to `assertThat(x).satisfies(..)` and `anyOf` to `satisfiesAnyOf`
103100
- org.openrewrite.java.testing.hamcrest.HamcrestOfMatchersToAssertJ
104101

105-
# Then remove calls to `MatcherAssert.assertThat(String, boolean)`
102+
# Convert `assertThat(x, is(value))` to `assertThat(x).isEqualTo(value)`
103+
- org.openrewrite.java.testing.hamcrest.HamcrestIsMatcherToAssertJ
104+
105+
# Convert `assertThat(reason, boolean)` to AssertJ
106106
- org.openrewrite.java.testing.hamcrest.AssertThatBooleanToAssertJ
107107

108108
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:

src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,49 @@ void bar(List<String> list) {
802802
);
803803
}
804804

805+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/858#issuecomment-3600925842")
806+
@Test
807+
void allOfWithIsAndNotNullValue() {
808+
rewriteRun(
809+
//language=java
810+
java(
811+
"""
812+
import org.junit.jupiter.api.Test;
813+
814+
import static org.hamcrest.MatcherAssert.assertThat;
815+
import static org.hamcrest.Matchers.allOf;
816+
import static org.hamcrest.Matchers.is;
817+
import static org.hamcrest.Matchers.notNullValue;
818+
819+
class ATest {
820+
@Test
821+
void test() {
822+
String str = "str";
823+
assertThat(str, allOf(notNullValue(), is("SQL")));
824+
}
825+
}
826+
""",
827+
"""
828+
import org.junit.jupiter.api.Test;
829+
830+
import static org.assertj.core.api.Assertions.assertThat;
831+
832+
class ATest {
833+
@Test
834+
void test() {
835+
String str = "str";
836+
assertThat(str)
837+
.satisfies(
838+
arg -> assertThat(arg).isNotNull(),
839+
arg -> assertThat(arg).isEqualTo("SQL")
840+
);
841+
}
842+
}
843+
"""
844+
)
845+
);
846+
}
847+
805848
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/526")
806849
@ParameterizedTest
807850
@ValueSource(

0 commit comments

Comments
 (0)