Json路径不匹配。预期sql.Date,找到lang.String(RestAssure)

时间:2016-06-07 07:42:44

标签: java json rest rest-assured

我对响应体的反序列化存在问题。我需要sql.Date但实际上我得到了lang.String 请帮我正确设置我的RestAssure。

以下是JsonObject& root = jsonBuffer.parseObject(json); test( root ); void test(JsonObject &root) { // Pass by reference. int flag = root["success"]; } 看起来的样子:

RestAssure.config

这是我的测试:

RestAssured.config =
    RestAssuredConfig.config().objectMapperConfig(objectMapperConfig()
    .gsonObjectMapperFactory((aClass, s) -> 
    new GsonBuilder().setDateFormat("yyyy-MM-dd").create()));

我得到了这样的例外:

given() 
       .contentType("application/json") 
       .when() .get("some url") .then()
       .assertThat().body("birthday", response ->
       equalTo(Date.valueOf("2016-06-07")))

1 个答案:

答案 0 :(得分:1)

在响应中使用extract()获取所需的值并将字符串转换为您想要的任何值。

Response r = given() 
   .contentType("application/json") 
   .when() .get("some url") 
   .then()
   .extract();
相关问题