Is there a Hamcrest Matcher that cleanly lets me assert that the result of a method, that returns a Collection of objects, has at least one object that contains a property with certain value?
For example:
class Person {
private String name;
}
The method under test returns a collection of Person.
I need to assert that at least one Person is called Peter.
First, you need to create a
Matcherthat can match aPerson's name. Then, you could use hamcrest'sCoreMatchers#hasItemto check if aCollectionhas an item this mathcer matches.Personally, I like to declare such matchers anonymously in
staticmethods as a sort of syntactic sugaring: