簡體   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