How do I get the value of a key if it contains a space in Rest Assured / Serenity?

风格不统一 提交于 2019-12-11 04:45:52

问题


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?


回答1:


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()));


来源:https://stackoverflow.com/questions/45112425/how-do-i-get-the-value-of-a-key-if-it-contains-a-space-in-rest-assured-serenit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!