Suppose I want a unit test method with this signature:
List<MyItem> getMyItems();
Suppose MyItem is a Pojo with many properties, one of which is "name" , accessed via getName() .
All I need to check is that List<MyItem> or any Iterable contains two instances of MyItem whose "name" properties have the values "foo" and "bar" . If any other properties do not match, I absolutely do not need the goals of this test. If the names match, this is a successful test.
I would like it to be single line, if possible. Here is some kind of "pseudo-syntax" of what I would like to do.
assert(listEntriesMatchInAnyOrder(myClass.getMyItems(), property("name"), new String[]{"foo", "bar"});
Can Hamcrest be good for this kind of thing? If so, what exactly will be the hamcrest version of my pseudo-syntax above?
java unit-testing junit4 hamcrest
Kevin Pauli Aug 28 2018-12-12T00: 00Z
source share