Skip to content

Commit

Permalink
Simplify toString assertion (#554)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Jul 24, 2024
1 parent 399931b commit c2e4fba
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/rewrite/assertj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/META-INF/rewrite/junit5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,40 @@ void test(Iterator<String> iterator, Iterator<String> 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");
}
}
"""
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,7 +43,7 @@ void assertTrueComparisonNullToAssertNull() {
"""
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class ExampleTest {
@Test
void test() {
Expand Down Expand Up @@ -105,7 +104,7 @@ void assertFalseNegatedEqualsNullToAssertNull() {
"""
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class ExampleTest {
@Test
void test() {
Expand Down Expand Up @@ -136,7 +135,7 @@ void assertNotEqualsInverted() {
"""
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class A {
class B {}
Expand All @@ -151,7 +150,7 @@ public void FlippedParams(){
"""
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class A {
class B {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit c2e4fba

Please sign in to comment.