One way to do this is to pass a List
of Matcher
s to contains
:
List<Matcher<Object>> expected = Arrays.stream(ids)
.mapToObj(id -> hasProperty("id", equalTo(id)))
.collect(Collectors.toList());
assertThat(entities, contains(expected));
If you plan to do this a lot, you can put it in a helper method:
private static <E> Matcher<Iterable<? extends E>> containsWithProperty(String propertyName, List<?> propertyValues) {
List<Matcher<? super E>> itemMatchers = propertyValues.stream()
.map(value -> hasProperty(propertyName, equalTo(value)))
.collect(Collectors.toList());
return contains(itemMatchers);
}
Usage:
// Convert ids array to a List
List<Long> idList = Arrays.stream(ids)
.boxed()
.collect(Collectors.toList());
assertThat(entities, containsWithProperty("id", idList));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…