繁体   English   中英

在REST Assured中,如何检查响应中是否存在字段?

[英]In REST Assured, how can I check if a field is present or not in the response?

我怎样才能确保我的响应(包括它是JSON) 包含不包含特定字段?

when()
    .get("/person/12345")
.then()
    .body("surname", isPresent()) // Doesn't work...
    .body("age", isNotPresent()); // ...But that's the idea.

我正在寻找一种方法来断言我的JSON是否包含字段agesurname

您也可以在JSON字符串上使用Hamcrest匹配器hasKey() (来自org.hamcrest.Matchers类)。

when()
    .get("/person/12345")
.then()
    .body("$", hasKey("surname"))
    .body("$", not(hasKey("age")));

您可以像这样使用equals(null):

.body("surname", equals(null))

如果该字段不存在,则它将为null

检查wheedver字段是否为空。 例如:

    Response resp = given().contentType("application/json").body(requestDto).when().post("/url");
    ResponseDto response = resp.then().statusCode(200).as(ResponseDto.class);
    Assertions.assertThat(response.getField()).isNotNull();

使用: import static org.junit.Assert.assertThat;

然后你可以: assertThat(response.then().body(containsString("thingThatShouldntBeHere")), is(false));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM