Describe the bug
When comparingOnlyFields() is used on a list of objects to recursively compare object properties it never reports a failure.
I would have expected it to behave like a complement to ignoringFields() (which works as expected on a list of objects).
- assertj core version: 3.24.2
- java version: Temurin-17.0.6+10 (build 17.0.6+10)
- test framework version: junit-jupiter:5.9.1
Test case reproducing the bug
package foo;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class FooTest {
public class Person {
String firstname;
String lastname;
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
@Test
public void listExample() {
Person foo = new Person("Foo First", "Foo Last");
Person bar = new Person("Bar First", "Bar Last");
Person expectedFoo = new Person("Foo First", "wrong");
Person expectedBar = new Person("Bar First", "wrong");
List<Person> people = Arrays.asList(foo, bar);
List<Person> expectedPeople = Arrays.asList(expectedFoo, expectedBar);
// Works
assertThat(people)
.usingRecursiveComparison()
.ignoringFields("lastname")
.isEqualTo(expectedPeople);
// Fails as expected
assertThat(people)
.usingRecursiveComparison()
.ignoringFields("firstname")
.isEqualTo(expectedPeople);
// Should fail - but works!
assertThat(people)
.usingRecursiveComparison()
.comparingOnlyFields("lastname")
.isEqualTo(expectedPeople);
// Should also fail - but works!
assertThat(people)
.usingRecursiveComparison()
.comparingOnlyFields("bla", "blub")
.isEqualTo(expectedPeople);
}
}
Describe the bug
When
comparingOnlyFields()is used on a list of objects to recursively compare object properties it never reports a failure.I would have expected it to behave like a complement to
ignoringFields()(which works as expected on a list of objects).Test case reproducing the bug