Niel de Wet
Niel de Wet

Reputation: 8398

Shazamcrest custom matching with collections

With Shazamcrest is is possible to do custom matching by supplying a field path and a matcher that is then applied to that field.

For example[cf]:

assertThat(
  actualPerson,
  sameBeanAs(expectedPerson).with("address.streetName", startsWith("Via"));

How do you do the same if you are matching collections? Can you apply a custom matcher to each object in the collection? To all objects in the collection?

Upvotes: 1

Views: 713

Answers (1)

David Harkness
David Harkness

Reputation: 36532

Assuming Shazamcrest matchers work just like regular Hamcrest matchers, you should be able to use them with all of the built-in collection matchers.

assertThat(
  people,
  contains(
    sameBeanAs(expectedPerson).with("address.streetName", startsWith("Via")
  )
);

The documentation says that you must use a custom assertThat from the package. While that's only for the failure descriptions, it may interfere with using some matchers.

Upvotes: 2

Related Questions