Validate if response at the root element contains partially an expected json

1,497 views
Skip to first unread message

David Prévost

unread,
Oct 9, 2018, 11:58:34 AM10/9/18
to REST assured
I'm doing some BDD test using Rest-Assured and I would like to validate that a json response contains at least some given fields with theirs corresponding values.

I was able to achieve this validation using assert-j below.
1. I parse the root response in a map of fields/values
2. I do the same for the expected json fields/values
3. I use assertj to validate if the response map contains the expected map of fields/values.
Map<String, Object> actual = JsonPath.from(restAssuredResponse.body().asString()).getMap(ROOT);

String expectedJson = readJsonFromFile(fileName);
Map<String, Object> expected = JsonPath.from(expectedJson).getMap("");

assertThat(actual).containsAllEntriesOf(expected);


However, I would like to use the Rest-Assured api intead of parsing the response and my expected result. In order words, I would like to have something similar to:

String expectedJson = readJsonFromFile(fileName);
Map<String, Object> expected = JsonPath.from(expectedJson).getMap(ROOT);

restAssuredResponse.then().assertThat().body(containsAllEntriesOf(expected));

Or
String expectedJson = readJsonFromFile(fileName);

restAssuredResponse.then().assertThat().body(containsAllEntriesOf(expectedJson));

I tried something like below 
String expectedJson = readJsonFromFile(fileName);
Map<String, Object> expected = JsonPath.from(expectedJson).getMap(ROOT);

restAssuredResponse.then().body(containsInAnyOrder(expected));

but I got the error:

java.lang.AssertionError: 1 expectation failed.
Response body doesn't match expectation.
Expected: iterable over [<{date_time=2017-12-25T20:00:00Z>] in any order
  Actual: {"event_id":"1260366", "date_time":"2017-12-25T20:00:00Z"}


Does anyone has some idea?

Thanks

David

David Prévost

unread,
Oct 9, 2018, 6:14:59 PM10/9/18
to REST assured
After some digging, I found 2 solutions

1. To check if the complete response contains some json we can do this (using hamcrest-json):
String expectedJson = readJsonFromFile(expectedFileNameOrJsonContent);

restAssuredResponse.then().assertThat()
.body(SameJSONAs.sameJSONAs(expectedJson).allowingAnyArrayOrdering().allowingExtraUnexpectedFields());

However, this works only when dealing with the complete body, if you try the same thing for a given path it will not work. So when given a path we can use something like:

String expectedJson = readJsonFromFile(expectedFileNameOrJsonContent);

restAssuredResponse.then().assertThat().body(path,
hasItems(JsonPath.from(expectedJson).getMap(ROOT)));

This bring to the second solution where if the path is "" then it do the same as the first solution.

I would have like to have the first solution works also when given a path but the second one is also pretty good.

Hope It can helps others!


Johan Haleby

unread,
Oct 10, 2018, 6:13:24 AM10/10/18
to rest-a...@googlegroups.com
Thanks for sharing your solution David!

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Prévost

unread,
Oct 10, 2018, 6:19:58 PM10/10/18
to rest-a...@googlegroups.com
I did a small mistake below is the working version. Note that the cast to Object is required else I'm getting this error: "java.lang.ClassCastException: java.util.HashMap cannot be cast to [Lorg.hamcrest.Matcher"
String expectedJson = readJsonFromFile(expectedFileNameOrJsonContent);

restAssuredResponse.then().assertThat().body(path,
    hasItems((Object)JsonPath.from(expectedJson).get(ROOT)));
Reply all
Reply to author
Forward
0 new messages