Hamcrest compare collections
If you want to assert that the two lists are identical, don't complicate things with Hamcrest:
assertEquals(expectedList, actual.getList());
If you really intend to perform an order-insensitive comparison, you can call the containsInAnyOrder
varargs method and provide values directly:
assertThat(actual.getList(), containsInAnyOrder("item1", "item2"));
(Assuming that your list is of String
, rather than Agent
, for this example.)
If you really want to call that same method with the contents of a List
:
assertThat(actual.getList(), containsInAnyOrder(expectedList.toArray(new String[expectedList.size()]));
Without this, you're calling the method with a single argument and creating a Matcher
that expects to match an Iterable
where each element is a List
. This can't be used to match a List
.
That is, you can't match a List<Agent>
with a Matcher<Iterable<List<Agent>>
, which is what your code is attempting.
To complement @Joe's answer:
Hamcrest provides you with three main methods to match a list:
contains
Checks for matching all the elements taking in count the order, if the list has more or less elements, it will fail
containsInAnyOrder
Checks for matching all the elements and it doesn't matter the order, if the list has more or less elements, will fail
hasItems
Checks just for the specified objects it doesn't matter if the list has more
hasItem
Checks just for one object it doesn't matter if the list has more
All of them can receive a list of objects and use equals
method for comparation or can be mixed with other matchers like @borjab mentioned:
assertThat(myList , contains(allOf(hasProperty("id", is(7L)), hasProperty("name", is("testName1")), hasProperty("description", is("testDesc1"))), allOf(hasProperty("id", is(11L)), hasProperty("name", is("testName2")), hasProperty("description", is("testDesc2")))));
http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#contains(E...)http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#containsInAnyOrder(java.util.Collection)http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#hasItems(T...)