【问题标题】:Problems matching a long value in Rest Assured json body在 Rest Assured json 正文中匹配长值的问题
【发布时间】:2018-10-15 12:42:23
【问题描述】:

我有以下回应:

[
    {
        "id": 53,
        "fileUri": "abc",
        "filename": "abc.jpg",
        "fileSizeBytes": 578466,
        "createdDate": "2018-10-15",
        "updatedDate": "2018-10-15"
    },
    {
        "id": 54,
        "fileUri": "xyz",
        "filename": "xyz.pdf",
        "fileSizeBytes": 88170994,
        "createdDate": "2018-10-15",
        "updatedDate": "2018-10-15"
    }
]

我正在尝试将 id 值与 JUnit 中的对象匹配,如下所示:

RestAssured.given() //
                .expect() //
                .statusCode(HttpStatus.SC_OK) //
                .when() //
                .get(String.format("%s/%s/file", URL_BASE, id)) //
                .then() //
                .log().all() //
                .body("", hasSize(2)) //
                .body("id", hasItems(file1.getId(), file2.getId()));

但是当匹配发生时,它会尝试将int 匹配到long。相反,我得到了这个输出:

java.lang.AssertionError: 1 expectation failed.
JSON path id doesn't match.
Expected: (a collection containing <53L> and a collection containing <54L>)
  Actual: [53, 54]

如何告诉 Rest Assured 该值确实很长,即使它可能短到可以放入 int 中?我可以将文件的 id 转换为 int 并且它可以工作,但这似乎很草率。

【问题讨论】:

    标签: testing rest-assured rest-assured-jsonpath


    【解决方案1】:

    问题是从json转换成java类型的时候,选择了int类型, 一种解决方案是比较 int 值。 而不是

    .body("id", hasItems(file1.getId(), file2.getId()));
    

    使用

    .body("id", hasItems(new Long(file1.getId()).intValue(), new Long(file2.getId()).intValue()));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 2018-06-17
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多