0

我正在起诉让我的 api 自动化,这是我的 jsonResponse:-

{
    "al": [{
            "aid": 1464,
            "_r": "Bus Stand,",
            "_l": "spaze it park2,",
            "_c": ",",
            "_s": "Haryana,",
            "co": "India,",
            "pc": "122001",
            "fa": "Sona Road, spaze it park, Gurgaon, Haryana,",
            "fn": "225,",
            "lm": "omax mall",
            "pa": null,
            "at": 1
        },
        {
            "aid": 1462,
            "_r": "Bus Stand,",
            "_l": "spaze it park2,",
            "_c": "Gurgaon,",
            "_s": "Haryana,",
            "co": "India,",
            "pc": "122001",
            "fa": "Sona Road, spaze it park, Gurgaon, Haryana,",
            "fn": "225,",
            "lm": "omax mall",
            "pa": null,
            "at": 1
        },
        {
            "aid": 1461,
            "_r": null,
            "_l": null,
            "_c": "Gurgaon1",
            "_s": "",
            "co": null,
            "pc": "122003",
            "fa": "Gurgaon, HRyana, 122003",
            "fn": "",
            "lm": "",
            "pa": null,
            "at": -1
        },
        {
            "aid": 1460,
            "_r": "Bus Stand,",
            "_l": "spaze it park2,",
            "_c": "Gurgaon,",
            "_s": "Haryana,",
            "co": "India,",
            "pc": "122001",
            "fa": "Sona Road, spaze it park, Gurgaon, Haryana,",
            "fn": "225,",
            "lm": "omax mall",
            "pa": null,
            "at": 2
        }
    ]
}

现在我想为具有“aid”的 jasonarray 断言:1460,比如 lm 的值是多少,at 等参数。我们如何才能放心地做同样的事情。也想知道jsonarray的索引位置有“aid”:1460。请帮帮我!!

4

2 回答 2

1

在内置断言中使用 REST Assured 测试 JSON 响应相当容易,但在您的情况下,您还需要数组的索引号,我不确定它是否可以找到。我认为这些方面的内容会有所帮助

Response res = given().
            get(url).
            then().
            statusCode(200).
            extract().
            response();


JsonArray arr=new JsonParser().parse(res.jsonPath().get("al").toString()).getAsJsonArray();

然后,您可以使用 JSONArray 找出索引以及相应的元素。

于 2018-03-21T10:45:56.897 回答
0

关于断言,您可以使用 RestAssured 内置GPath查询语法:

RestAssured.given()
        .baseUri("http://your.server.com")
        .accept(ContentType.JSON)
        .get("/jsonFile")
        .then()
        .statusCode(200)
        .body("al.findIndexOf { it.aid == 1461 }", is(2))       // findIndexOf returns zero-based list index
        .body("al.find { it.aid == 1461 }._c", is("Gurgaon1"))
        .body("al.find { it.aid == 1461 }.pc", is("122003"));
于 2018-03-25T13:20:20.733 回答