From 7fb2c9d4e8158df1649e816d8e2ccd18db775e2e Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 13 Mar 2024 11:23:02 +0100 Subject: [PATCH] Convert Hamcrest `not(hasItem(item))` to AssertJ `doesNotContain(item)` https://github.com/openrewrite/rewrite-testing-frameworks/issues/357 --- .../resources/META-INF/rewrite/hamcrest.yml | 4 + .../HamcrestNotMatcherToAssertJTest.java | 162 ++++++++++++------ 2 files changed, 110 insertions(+), 56 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/hamcrest.yml b/src/main/resources/META-INF/rewrite/hamcrest.yml index a9e2f91b7..16209981e 100644 --- a/src/main/resources/META-INF/rewrite/hamcrest.yml +++ b/src/main/resources/META-INF/rewrite/hamcrest.yml @@ -267,6 +267,10 @@ recipeList: notMatcher: emptyString assertion: isNotEmpty + - org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ: + notMatcher: hasItem + assertion: doesNotContain + # Add dependency if not already present - org.openrewrite.java.dependencies.AddDependency: groupId: org.assertj diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestNotMatcherToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestNotMatcherToAssertJTest.java index 9488c74e1..fb5779ef2 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestNotMatcherToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestNotMatcherToAssertJTest.java @@ -42,35 +42,38 @@ void notMatcher() { spec -> spec.recipe(new HamcrestNotMatcherToAssertJ("equalTo", "isNotEqualTo")), //language=java java( - """ - import org.junit.jupiter.api.Test; - - import static org.hamcrest.MatcherAssert.assertThat; - import static org.hamcrest.Matchers.not; - import static org.hamcrest.Matchers.equalTo; - - class ATest { - @Test - void test() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - assertThat(str1, not(equalTo(str2))); - } - } - """, """ - import org.junit.jupiter.api.Test; - - import static org.assertj.core.api.Assertions.assertThat; - - class ATest { - @Test - void test() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - assertThat(str1).isNotEqualTo(str2); - } - } - """)); + """ + import org.junit.jupiter.api.Test; + + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.not; + import static org.hamcrest.Matchers.equalTo; + + class ATest { + @Test + void test() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + assertThat(str1, not(equalTo(str2))); + } + } + """, + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void test() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + assertThat(str1).isNotEqualTo(str2); + } + } + """ + ) + ); } @Test @@ -79,32 +82,79 @@ void notMatcherWithReason() { spec -> spec.recipe(new HamcrestNotMatcherToAssertJ("nullValue", "isNotNull")), //language=java java( - """ - import org.junit.jupiter.api.Test; - - import static org.hamcrest.MatcherAssert.assertThat; - import static org.hamcrest.Matchers.not; - import static org.hamcrest.Matchers.nullValue; - - class ATest { - @Test - void test() { - String str1 = "Hello world!"; - assertThat("Reason", str1, not(nullValue())); - } - } - """, """ - import org.junit.jupiter.api.Test; - - import static org.assertj.core.api.Assertions.assertThat; - - class ATest { - @Test - void test() { - String str1 = "Hello world!"; - assertThat(str1).as("Reason").isNotNull(); - } - } - """)); + """ + import org.junit.jupiter.api.Test; + + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.not; + import static org.hamcrest.Matchers.nullValue; + + class ATest { + @Test + void test() { + String str1 = "Hello world!"; + assertThat("Reason", str1, not(nullValue())); + } + } + """, + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void test() { + String str1 = "Hello world!"; + assertThat(str1).as("Reason").isNotNull(); + } + } + """ + ) + ); + } + + @Test + void notMatcherWithCollection() { + rewriteRun( + spec -> spec.recipe(new HamcrestNotMatcherToAssertJ("hasItem", "doesNotContain")), + //language=java + java( + """ + import java.util.List; + + import org.junit.jupiter.api.Test; + + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.not; + import static org.hamcrest.Matchers.hasItem; + + class ATest { + @Test + void test() { + List str1 = List.of("Hello world!"); + String str2 = "Hello world!"; + assertThat(str1, not(hasItem(str2))); + } + } + """, + """ + import java.util.List; + + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void test() { + List str1 = List.of("Hello world!"); + String str2 = "Hello world!"; + assertThat(str1).doesNotContain(str2); + } + } + """ + ) + ); } } \ No newline at end of file