usingRecursiveComparison with usingOverriddenEquals ignores equals method of the compared object
See original GitHub issueSummary
Version 3.22.0 I expect if I use usingRecursiveComparison with usingOverriddenEquals, field “age” should be ignored. But as result I get
java.lang.AssertionError: Expecting actual: com.ComparatorDevices$Person@63be0d6 to be equal to: com.ComparatorDevices$Person@63be0b6 when recursively comparing field by field, but found the following difference:
field/property ‘age’ differ:
- actual value : “22”
- expected value: “11”
The recursive comparison was performed with this configuration:
- overridden equals methods were used in the comparison
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- 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).
Example
class Person {
String name;
String age;
public Person(String name, String age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
@Test()
public void personTest() {
Person expectedPerson = new Person("name", "11");
Person actualPerson = new Person("name", "22");
Assertions.assertThat(actualPerson)
.usingRecursiveComparison()
.usingOverriddenEquals()
.isEqualTo(expectedPerson);
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (8 by maintainers)
Top Results From Across the Web
RecursiveComparisonAssert (AssertJ fluent assertions 3.18.1 ...
This method instructs the recursive comparison to compare recursively all fields including the one whose type have overridden equals, except fields with java ......
Read more >Recursive comparison api improvements · Issue #1002 - GitHub
Users can specify the comparison to ignore all null fields in the object under ... to ignore overridden equals methods and compare objects...
Read more >AssertJ - fluent assertions java library - GitHub Pages
Fix usingRecursiveComparison with usingOverriddenEquals ignores equals method of the root object (#2479). (Yuta Saito). Fix error in javadoc. (Yuta Saito).
Read more >java - Strange behavior of assertThat(actual ... - Stack Overflow
I've come across an interesting behavior of recursive comparison in assertj library. If you compare objects of classes that are sub-classes, ...
Read more >org.assertj.core.api.RecursiveComparisonAssert Maven / Gradle / Ivy
Recursive comparison use of overridden {@code equals} methods * ... Makes the recursive comparison to ignore the given object under test fields.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Leave it open @uc4w6c, I’m looking at it and it might be that we don’t need
isRootObject
anymore (which you have removed in your PR). I’ll see if I can confirm that and if I do I’ll integrate your PR and you’ll get the credit for fixing this one 😃 (usually I get the PR code, play with it, refactor it if needed, squash all commits but keep the original PR author)@joel-costigliola Thank you 😃