assertj / assertj/assertj

Allow suppressing the "expected" and the "actual" values in failure messages, particularly in `RecursiveComparisonAssert`

Open
#3,656 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.8k
Forks
788
Avg merge
14h 57m
Merged PRs (30d)
36

Description

#### Feature summary

In `RecursiveComparisonAssert`, the method `isEqualTo` is currently hard-coded to employ the ErrorMessageFactory `ShouldBeEqualByComparingFieldByFieldRecursively`:

https://github.com/assertj/assertj/blob/4b280c46e987ae6771f12a5490854a9031b0750a/assertj-core/src/main/java/org/assertj/core/api/RecursiveComparisonAssert.java#L15

https://github.com/assertj/assertj/blob/4b280c46e987ae6771f12a5490854a9031b0750a/assertj-core/src/main/java/org/assertj/core/api/RecursiveComparisonAssert.java#L157

In practice, users may be interested in **customizing the error message**.

One real use case is when **comparing two large JSON files** (represented by a nested object of `Map`s and `List`s). In such scenarios, due to the default message always printing the whole "actual" and "expected" values...

https://github.com/assertj/assertj/blob/4b280c46e987ae6771f12a5490854a9031b0750a/assertj-core/src/main/java/org/assertj/core/error/ShouldBeEqualByComparingFieldByFieldRecursively.java#L37

... the assertion failure message may flood the terminal buffer, resulting in unreadable outputs, which goes against AssertJ's philosophy of being ergonomic.

#### Example

```java
assertThat(got)
.usingRecursiveComparison()
.isEqualTo(want);
```

This is what I see in IntelliJ IDEA -- Note how the message has been automatically shortened (with `<...>`), so I can't actually read what's wrong:

```
java.lang.AssertionError:
Expecting actual:
{"eee"="ffff"},
{"dddd"="cccc", "bbbb"="aaaa"<...>ompared with the following comparators:
- java.lang.Double -> org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration$$Lambda$2878/0x0000000802a995a0@4bda79d2
- java.lang.Float -> org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration$$Lambda$2878/0x0000000802a995a0@2a22ff7b
- java.lang.Integer -> org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration$$Lambda$2878/0x0000000802a995a0@122b5f62
- java.lang.Long -> org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration$$Lambda$2878/0x0000000802a995a0@8120f15
- java.nio.file.Path -> lexicographic comparator (Path natural order)
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
- the introspection strategy used was: DefaultRecursiveComparisonIntrospectionStrategy
```

My terminal emulator also can't handle this much output, resulting in me only able to see the last few lines of this message.

When running this assertion in a CI environment, whenever it fails, the console log page would be unable to render properly in my browser due to the sheer amount of text.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.