From 8b347e7744a9c00657523690ec3e007a5b235d73 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 23 Feb 2026 21:20:34 +0100 Subject: [PATCH] Add missing Hamcrest `not(Matcher)` to AssertJ conversions (#866) Add 7 missing `HamcrestNotMatcherToAssertJ` entries for: - Map matchers: `not(hasKey)`, `not(hasValue)`, `not(hasEntry)`, `not(anEmptyMap)` - String matchers: `not(isEmptyOrNullString)`, `not(emptyOrNullString)` - Object matchers: `not(isA)` --- .../resources/META-INF/rewrite/hamcrest.yml | 24 +++++++++++ .../MigrateHamcrestToAssertJTest.java | 42 ++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/main/resources/META-INF/rewrite/hamcrest.yml b/src/main/resources/META-INF/rewrite/hamcrest.yml index 7130285f9..0b5c37627 100644 --- a/src/main/resources/META-INF/rewrite/hamcrest.yml +++ b/src/main/resources/META-INF/rewrite/hamcrest.yml @@ -382,3 +382,27 @@ recipeList: notMatcher: empty assertion: isNotEmpty + - org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ: + notMatcher: hasKey + assertion: doesNotContainKey + - org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ: + notMatcher: hasValue + assertion: doesNotContainValue + - org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ: + notMatcher: hasEntry + assertion: doesNotContainEntry + - org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ: + notMatcher: anEmptyMap + assertion: isNotEmpty + + - org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ: + notMatcher: isEmptyOrNullString + assertion: isNotEmpty + - org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ: + notMatcher: emptyOrNullString + assertion: isNotEmpty + + - org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ: + notMatcher: isA + assertion: isNotInstanceOf + 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 072f1a563..c5c483327 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java @@ -462,6 +462,43 @@ void test(String key, String value) { rewriteRun(java(before, after)); } + private static Stream notMapReplacements() { + return Stream.of( + Arguments.arguments("map1", "hasKey", "key", "doesNotContainKey"), + Arguments.arguments("map1", "hasValue", "value", "doesNotContainValue"), + Arguments.arguments("map1", "hasEntry", "key, value", "doesNotContainEntry"), + Arguments.arguments("map1", "anEmptyMap", "", "isNotEmpty") + ); + } + + @MethodSource("notMapReplacements") + @ParameterizedTest + void notMapReplacements(String actual, String hamcrestMatcher, String matcherArgs, String assertJAssertion) { + String importsBefore = """ + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.not; + import static org.hamcrest.Matchers.%s;""".formatted(hamcrestMatcher); + String importsAfter = "import static org.assertj.core.api.Assertions.assertThat;"; + //language=java + String template = """ + import java.util.Map; + import org.junit.jupiter.api.Test; + + %s + + class ATest { + @Test + void test(String key, String value) { + Map map1 = Map.of("a", "b", "c", "d"); + %s + } + } + """; + String before = template.formatted(importsBefore, "assertThat(%s, not(%s(%s)));".formatted(actual, hamcrestMatcher, matcherArgs)); + String after = template.formatted(importsAfter, "assertThat(%s).%s(%s);".formatted(actual, assertJAssertion, matcherArgs)); + rewriteRun(java(before, after)); + } + private static Stream notReplacements() { return Stream.of( Arguments.arguments("str1", "equalTo", "str2", "isNotEqualTo"), @@ -480,7 +517,10 @@ private static Stream notReplacements() { Arguments.arguments("str1", "equalToIgnoringCase", "str2", "isNotEqualToIgnoringCase"), Arguments.arguments("str1", "equalToIgnoringWhiteSpace", "str2", "isNotEqualToIgnoringWhitespace"), Arguments.arguments("str1", "blankString", "", "isNotBlank"), - Arguments.arguments("str1", "emptyString", "", "isNotEmpty") + Arguments.arguments("str1", "emptyString", "", "isNotEmpty"), + Arguments.arguments("str1", "isEmptyOrNullString", "", "isNotEmpty"), + Arguments.arguments("str1", "emptyOrNullString", "", "isNotEmpty"), + Arguments.arguments("str1", "isA", "String.class", "isNotInstanceOf") ); }