2017-03-27 56 views
7

我怎样才能确保我的回应,比如说是JSON,包含不包含的某个特定字段?在REST保证,如何检查字段是否存在或不在响应中?

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

我正在寻找一种方式来表明我的JSON是否包含与否领域年龄

回答

0

您可以使用等号(空)是这样的:

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

如果该字段不存在,这将是空

+2

这也匹配JSON字段设置为空。 –

+0

你是对的,但如果它收到零基本上是相同的 –

0

检查whetever字段为空。例如:

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

可以使用Hamcrest匹配器hasKey()的JSON字符串为好。

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

这是正确的答案,为什么不标记它呢? –

相关问题