Trying to compare two lists without considering some filed of elements, but can't find a solution.. How can I write using Hamcrest something like this?
assertThat(list, containsInAnyOrder(anotherList).ignoreFields("accountNumber"));
Trying to compare two lists without considering some filed of elements, but can't find a solution.. How can I write using Hamcrest something like this?
assertThat(list, containsInAnyOrder(anotherList).ignoreFields("accountNumber"));
If I understand correct, you want to check if two lists contains equal elements. And to check elements equality you want to compare elements by their fields values.
If so, you might take a look at equals(Object obj) and hashCode() methods of Object
class that are supposed to be used to check objects equality - and most libraries and frameworks invoke this methods to check if object "A" and object "B" are equal.
Short answer: First, override equals(Object obj)
and hashCode()
methods to check objects equality, or add some custom "comparator" function/class that would compare objects of your type. Then use containsInAnyOrder(T... items)
or containsInAnyOrder(Matcher<? super T>... itemMatchers)
methods depending on your choice at first step.