3

I am trying to use Rest Assured in the Serenity framework to validate an endpoint response. I send an xml body to the endpoint and expect a JSON response back like so:

{"Entry ID" : "654123"}

I want to send the XML and verify in the JSON response that the value of the key "Entry ID" is not empty or null. The problem is, the key has a space in it, and I believe it is causing an error. Here is what I have so far:

SerenityRest.given().contentType(ContentType.XML)
.body(xmlBody)
.when().accept(ContentType.JSON).post(endpoint)
.then().body("Entry ID", not(isEmptyOrNullString()))
.and().statusCode(200);

This produces the error:

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unable to resolve class                          Entry 
@ line 1, column 33.
                        Entry ID
                               ^

1 error

I have tried wrapping the "Entry ID" term in different ways to no avail:

.body("'Entry ID'", not(isEmptyOrNullString()))
.body("''Entry ID''", not(isEmptyOrNullString()))
.body("\"Entry ID\"", not(isEmptyOrNullString()))
.body("['Entry ID']", not(isEmptyOrNullString()))
.body("$.['Entry ID']", not(isEmptyOrNullString()))

Is it possible to get the value of a key that contains a space in Rest Assured?

Amax125
  • 31
  • 1
  • 4
  • Switch to Karate [ https://github.com/intuit/karate ] it does not have this issue, here is an example: https://github.com/intuit/karate/blob/8c27030c570fb5a42234e8cca4652cdb241c639b/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature#L26 – Peter Thomas Jul 18 '17 at 04:11
  • My thought is that something else must be wrong. body("['Entry ID']") is the correct way to do it though. https://stackoverflow.com/questions/34074569/apply-jsonpath-filter-to-field-with-space It works with jayway json path which is what restassured uses. Maybe try grabbing it using path instead and then verify it after? https://github.com/rest-assured/rest-assured/wiki/Usage#single-path – canpan14 Jul 18 '17 at 15:05

1 Answers1

5

You just need to escape the key with single quotes:

then().body("'Entry ID'", not(isEmptyOrNullString()))

Here's an example (tested in version 3.0.6):

// Given
String json = "{\"Entry ID\" : \"654123\"}";

// When
JsonPath jsonPath = JsonPath.from(json);

// Then
assertThat(jsonPath.getString("'Entry ID'"), not(isEmptyOrNullString()));
Johan
  • 37,479
  • 32
  • 149
  • 237