I'm using REST Assured with Hamcrest matchers in my tests:
given()
//...
.assertThat()
.body("id", arrayContaining(job1.getId(), job2.getId()))
.statusCode(200);
After running them I get the following error:
JSON path id doesn't match.
Expected: is [<70a1deec-ce17-4064-8037-5e546d3ed329>, <7ccd5ad0-7425-4df5-be64-bb8584da5d96>]
Actual: <[70a1deec-ce17-4064-8037-5e546d3ed329, 7ccd5ad0-7425-4df5-be64-bb8584da5d96]>
The response has following format:
[
{
"id": "some UUID 1"
"other_properties": ...
},
{
"id": "some UUID 1"
"other_properties": ...
}
]
My question is what triangle brackets mean? And which matcher is better to use. I know I probably just googled it wrong, but I've spent few days by googling and reading documentation and examining code on github. Yet still haven't find explanation about errors format. Thank you
The problem is not the selector, but your matcher.
arrayContaining
can only match against arrays, while contains
can match against any kind of Iterable
.
public static <E> Matcher<E[]> arrayContaining(E... items)
Creates a matcher for arrays that matches when each item in the examined array is logically equal to the corresponding item in the specified items. For a positive match, the examined array must be of the same length as the number of specified items. For example:
assertThat(new String[]{"foo", "bar"}, arrayContaining("foo", "bar"))
public static <E> Matcher<java.lang.Iterable<? extends E>> contains(E... items)
Creates a matcher for Iterables that matches when a single pass over the examined
Iterable
yields a series of items, each logically equal to the corresponding item in the specified items. For a positive match, the examined iterable must be of the same length as the number of specified items. For example:
assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"))
So change your assertion to .body("id", contains(job1.getId(), job2.getId()))
and your test should succeed.