Skip to content

Commit c2e4fba

Browse files
velotimtebeek
andauthored
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 <[email protected]> * Update src/test/java/org/openrewrite/java/testing/assertj/MigrateChainedAssertToAssertJTest.java * Minor fixes --------- Signed-off-by: Marvin Froeder <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent 399931b commit c2e4fba

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,11 @@ recipeList:
348348
assertToReplace: isFalse
349349
dedicatedAssertion: isExhausted
350350
requiredType: java.util.Iterator
351+
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertion:
352+
chainedAssertion: toString
353+
assertToReplace: isEqualTo
354+
dedicatedAssertion: hasToString
355+
requiredType: java.lang.Object
351356

352357
---
353358
type: specs.openrewrite.org/v1beta/recipe

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ recipeList:
247247
type: specs.openrewrite.org/v1beta/recipe
248248
name: org.openrewrite.java.testing.junit5.UpgradeOkHttpMockWebServer
249249
displayName: Use OkHttp 3 MockWebServer for JUnit 5
250-
description: Migrates OkHttp 3 `MockWebServer` to enable JUnit Jupiter Extension support
250+
description: Migrates OkHttp 3 `MockWebServer` to enable JUnit Jupiter Extension support.
251251
tags:
252252
- testing
253253
- junit
@@ -266,7 +266,7 @@ recipeList:
266266
type: specs.openrewrite.org/v1beta/recipe
267267
name: org.openrewrite.java.testing.junit5.CleanupAssertions
268268
displayName: Clean Up Assertions
269-
description: Simplifies JUnit Jupiter assertions to their most-direct equivalents
269+
description: Simplifies JUnit Jupiter assertions to their most-direct equivalents.
270270
tags:
271271
- testing
272272
- junit
@@ -286,7 +286,7 @@ recipeList:
286286
type: specs.openrewrite.org/v1beta/recipe
287287
name: org.openrewrite.java.testing.junit5.UseXMLUnitLegacy
288288
displayName: Use XMLUnit Legacy for JUnit 5
289-
description: Migrates XMLUnit 1.x to XMLUnit legacy 2.x
289+
description: Migrates XMLUnit 1.x to XMLUnit legacy 2.x.
290290
tags:
291291
- testing
292292
- junit

src/test/java/org/openrewrite/java/testing/assertj/MigrateChainedAssertToAssertJTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,40 @@ void test(Iterator<String> iterator, Iterator<String> otherIterator) {
455455
);
456456
}
457457
}
458+
459+
@Nested
460+
class Objects {
461+
@Test
462+
void objectoToStringReplacement() {
463+
rewriteRun(
464+
//language=java
465+
java(
466+
"""
467+
import org.junit.jupiter.api.Test;
468+
469+
import static org.assertj.core.api.Assertions.assertThat;
470+
471+
class MyTest {
472+
void testMethod(Object argument) {
473+
String s = "hello world";
474+
assertThat(argument.toString()).isEqualTo("value");
475+
}
476+
}
477+
""",
478+
"""
479+
import org.junit.jupiter.api.Test;
480+
481+
import static org.assertj.core.api.Assertions.assertThat;
482+
483+
class MyTest {
484+
void testMethod(Object argument) {
485+
String s = "hello world";
486+
assertThat(argument).hasToString("value");
487+
}
488+
}
489+
"""
490+
)
491+
);
492+
}
493+
}
458494
}

src/test/java/org/openrewrite/java/testing/junit5/CleanupAssertionsTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.InMemoryExecutionContext;
21-
import org.openrewrite.config.Environment;
2221
import org.openrewrite.java.JavaParser;
2322
import org.openrewrite.test.RecipeSpec;
2423
import org.openrewrite.test.RewriteTest;
@@ -44,7 +43,7 @@ void assertTrueComparisonNullToAssertNull() {
4443
"""
4544
import org.junit.jupiter.api.Assertions;
4645
import org.junit.jupiter.api.Test;
47-
46+
4847
class ExampleTest {
4948
@Test
5049
void test() {
@@ -105,7 +104,7 @@ void assertFalseNegatedEqualsNullToAssertNull() {
105104
"""
106105
import org.junit.jupiter.api.Assertions;
107106
import org.junit.jupiter.api.Test;
108-
107+
109108
class ExampleTest {
110109
@Test
111110
void test() {
@@ -136,7 +135,7 @@ void assertNotEqualsInverted() {
136135
"""
137136
import org.junit.jupiter.api.Assertions;
138137
import org.junit.jupiter.api.Test;
139-
138+
140139
class A {
141140
class B {}
142141
@@ -151,7 +150,7 @@ public void FlippedParams(){
151150
"""
152151
import org.junit.jupiter.api.Assertions;
153152
import org.junit.jupiter.api.Test;
154-
153+
155154
class A {
156155
class B {}
157156

src/test/java/org/openrewrite/java/testing/junit5/UseXMLUnitLegacyTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.openrewrite.java.testing.junit5;
1717

1818
import org.junit.jupiter.api.Test;
19-
import org.openrewrite.config.Environment;
2019
import org.openrewrite.test.RecipeSpec;
2120
import org.openrewrite.test.RewriteTest;
2221

@@ -27,10 +26,7 @@ class UseXMLUnitLegacyTest implements RewriteTest {
2726
@Override
2827
public void defaults(RecipeSpec spec) {
2928
spec
30-
.recipe(Environment.builder()
31-
.scanRuntimeClasspath()
32-
.build()
33-
.activateRecipes("org.openrewrite.java.testing.junit5.UseXMLUnitLegacy"));
29+
.recipeFromResources("org.openrewrite.java.testing.junit5.UseXMLUnitLegacy");
3430
}
3531

3632
@Test

0 commit comments

Comments
 (0)