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
Matcher
that can match aPerson
's name. Then, you could use hamcrest'sCoreMatchers#hasItem
to check if aCollection
has an item this mathcer matches.Personally, I like to declare such matchers anonymously in
static
methods as a sort of syntactic sugaring: