本文介绍了如果密钥在“确保放心/宁静"中包含空格,如何获得密钥的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Serenity框架中使用Rest Assured来验证端点响应.我将xml正文发送到端点,并希望像这样返回JSON响应:

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"}

我想发送XML并在JSON响应中验证键"Entry ID"的值不为空或为null.问题是,密钥中有空格,并且我相信它会导致错误.这是我到目前为止的内容:

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

这会产生错误:

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

1 error

我尝试用不同的方式包装条目ID"一词,但无济于事:

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?

推荐答案

您只需要用单引号将键转义:

You just need to escape the key with single quotes:

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

这是一个示例(在3.0.6版中测试):

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

这篇关于如果密钥在“确保放心/宁静"中包含空格,如何获得密钥的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:01