Testing content of list ignoring some of the fields
Give a try to AssertJ's usingElementComparatorIgnoringFields:
Employee bill = new Employee("Bill", 60, "Micro$oft");
Employee appleBill = new Employee("Billie", 60, "Apple");
List<Employee> employees = newArrayList(bill, appleBill);
Employees[] expectedEmployees = { new Employee("Bill", 60, "Google"),
new Employee("Billie", 60, "Facebook") };
// this assertion succeeds as we don't compare the company field.
assertThat(employees).usingElementComparatorIgnoringFields("company")
.contains(expectedEmployees);
edit: The new recursive comparison API can be used, it gives much finer control about what is being compared: https://assertj.github.io/doc/#assertj-core-recursive-comparison-ignoring-fields
In my case I tried to compare lists of different classes. @Joel Costigliola hinted me to use usingElementComparatorIgnoringFields
, so I wrote this code:
List<ClassOne> input = new ArrayList<>();
input.add(...);
...
List<ClassTwo> result = new ArrayList<>();
...
assertThat(result).usingElementComparatorIgnoringFields("field1", "field2").isEqualTo(input);