Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tslmy opened this issue Nov 13, 2024 · 3 comments

Comments

@tslmy
Copy link

tslmy commented Nov 13, 2024

Feature summary

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

import static org.assertj.core.error.ShouldBeEqualByComparingFieldByFieldRecursively.shouldBeEqualByComparingFieldByFieldRecursively;

if (!differences.isEmpty()) throw objects.getFailures().failure(info, shouldBeEqualByComparingFieldByFieldRecursively(actual,

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 Maps and Lists). In such scenarios, due to the default message always printing the whole "actual" and "expected" values...

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

Example

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.

@joel-costigliola
Copy link
Member

joel-costigliola commented Nov 17, 2024

@tslmy if you are comparing JSON document, have you tried using https://github.com/lukas-krecan/JsonUnit ?

That might give you a more precise error.

@joel-costigliola
Copy link
Member

You can also use overridingErrorMessage to change the error message as you like, see the corresponding javadoc.

@tslmy
Copy link
Author

tslmy commented Nov 17, 2024

Thanks for the recommendation. Let me try tomorrow. I wasn't aware of that library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants