From c2e4fba4b7061b7d4ebac0a7a2fbde7438b7b4f9 Mon Sep 17 00:00:00 2001 From: Marvin Date: Wed, 24 Jul 2024 09:28:01 +0000 Subject: [PATCH] Simplify toString assertion (#554) * Simplify toString assertion Instead of using: ``` assertThat(cat.toString()).isEqualTo("animal"); ``` Could use: ``` assertThat(cat).hasToString("animal"); ``` Signed-off-by: Marvin Froeder * Update src/test/java/org/openrewrite/java/testing/assertj/MigrateChainedAssertToAssertJTest.java * Minor fixes --------- Signed-off-by: Marvin Froeder Co-authored-by: Tim te Beek Co-authored-by: Tim te Beek --- .../resources/META-INF/rewrite/assertj.yml | 5 +++ .../resources/META-INF/rewrite/junit5.yml | 6 ++-- .../MigrateChainedAssertToAssertJTest.java | 36 +++++++++++++++++++ .../testing/junit5/CleanupAssertionsTest.java | 9 +++-- .../testing/junit5/UseXMLUnitLegacyTest.java | 6 +--- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index f7ed519c6..68358ba0c 100644 --- a/src/main/resources/META-INF/rewrite/assertj.yml +++ b/src/main/resources/META-INF/rewrite/assertj.yml @@ -348,6 +348,11 @@ recipeList: assertToReplace: isFalse dedicatedAssertion: isExhausted requiredType: java.util.Iterator + - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion: + chainedAssertion: toString + assertToReplace: isEqualTo + dedicatedAssertion: hasToString + requiredType: java.lang.Object --- type: specs.openrewrite.org/v1beta/recipe diff --git a/src/main/resources/META-INF/rewrite/junit5.yml b/src/main/resources/META-INF/rewrite/junit5.yml index 8dc44613d..6d889a85a 100755 --- a/src/main/resources/META-INF/rewrite/junit5.yml +++ b/src/main/resources/META-INF/rewrite/junit5.yml @@ -247,7 +247,7 @@ recipeList: type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.testing.junit5.UpgradeOkHttpMockWebServer displayName: Use OkHttp 3 MockWebServer for JUnit 5 -description: Migrates OkHttp 3 `MockWebServer` to enable JUnit Jupiter Extension support +description: Migrates OkHttp 3 `MockWebServer` to enable JUnit Jupiter Extension support. tags: - testing - junit @@ -266,7 +266,7 @@ recipeList: type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.testing.junit5.CleanupAssertions displayName: Clean Up Assertions -description: Simplifies JUnit Jupiter assertions to their most-direct equivalents +description: Simplifies JUnit Jupiter assertions to their most-direct equivalents. tags: - testing - junit @@ -286,7 +286,7 @@ recipeList: type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.testing.junit5.UseXMLUnitLegacy displayName: Use XMLUnit Legacy for JUnit 5 -description: Migrates XMLUnit 1.x to XMLUnit legacy 2.x +description: Migrates XMLUnit 1.x to XMLUnit legacy 2.x. tags: - testing - junit diff --git a/src/test/java/org/openrewrite/java/testing/assertj/MigrateChainedAssertToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/assertj/MigrateChainedAssertToAssertJTest.java index 99bb27790..51d071685 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/MigrateChainedAssertToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/assertj/MigrateChainedAssertToAssertJTest.java @@ -455,4 +455,40 @@ void test(Iterator iterator, Iterator otherIterator) { ); } } + + @Nested + class Objects { + @Test + void objectoToStringReplacement() { + rewriteRun( + //language=java + java( + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + void testMethod(Object argument) { + String s = "hello world"; + assertThat(argument.toString()).isEqualTo("value"); + } + } + """, + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + void testMethod(Object argument) { + String s = "hello world"; + assertThat(argument).hasToString("value"); + } + } + """ + ) + ); + } + } } diff --git a/src/test/java/org/openrewrite/java/testing/junit5/CleanupAssertionsTest.java b/src/test/java/org/openrewrite/java/testing/junit5/CleanupAssertionsTest.java index 0553fe906..1d4a95426 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/CleanupAssertionsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/CleanupAssertionsTest.java @@ -18,7 +18,6 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; -import org.openrewrite.config.Environment; import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; @@ -44,7 +43,7 @@ void assertTrueComparisonNullToAssertNull() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class ExampleTest { @Test void test() { @@ -105,7 +104,7 @@ void assertFalseNegatedEqualsNullToAssertNull() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class ExampleTest { @Test void test() { @@ -136,7 +135,7 @@ void assertNotEqualsInverted() { """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class A { class B {} @@ -151,7 +150,7 @@ public void FlippedParams(){ """ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - + class A { class B {} diff --git a/src/test/java/org/openrewrite/java/testing/junit5/UseXMLUnitLegacyTest.java b/src/test/java/org/openrewrite/java/testing/junit5/UseXMLUnitLegacyTest.java index 902f8fa99..5cefde039 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UseXMLUnitLegacyTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UseXMLUnitLegacyTest.java @@ -16,7 +16,6 @@ package org.openrewrite.java.testing.junit5; import org.junit.jupiter.api.Test; -import org.openrewrite.config.Environment; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; @@ -27,10 +26,7 @@ class UseXMLUnitLegacyTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec - .recipe(Environment.builder() - .scanRuntimeClasspath() - .build() - .activateRecipes("org.openrewrite.java.testing.junit5.UseXMLUnitLegacy")); + .recipeFromResources("org.openrewrite.java.testing.junit5.UseXMLUnitLegacy"); } @Test