代码之家  ›  专栏  ›  技术社区  ›  CodeChimp

在Rest Assured json正文中匹配长值时出现问题

  •  0
  • CodeChimp  · 技术社区  · 7 年前

    [
        {
            "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?我可以把文件的 int

    0 回复  |  直到 7 年前
        1
  •  -1
  •   vimuth    7 年前

    问题是,当从json转换为java类型时,选择int-type, 一种解决方案是比较int值。 而不是

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

    使用

    .body("id", hasItems(new Long(file1.getId()).intValue(), new Long(file2.getId()).intValue()));
    
    推荐文章