在Rest Assured json体中匹配长值的问题

问题描述 投票:0回答:1

我有以下回应:

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

testing rest-assured rest-assured-jsonpath
1个回答
0
投票

问题是当从json转换为java类型时,选择了int类型,一种解决方案是比较int值。代替

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

使用

.body("id", hasItems(new Long(file1.getId()).intValue(), new Long(file2.getId()).intValue()));
© www.soinside.com 2019 - 2024. All rights reserved.