提问者:小点点

在Rest保证json主体中匹配长值的问题


我有以下回应:

[
    {
        "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()));

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

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保证该值确实是一个长值,即使它可能足够短以适合int?我可以将文件的id转换为int并且它可以工作,但这似乎很草率。


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