0

I am suing restassured for automating my apis, here is my 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
        }
    ]
}

Now i want to put assertions for jasonarray having "aid": 1460, like what's the value of lm,at etc parameters. How we can do the same in restassured. Also want to know the index position of jsonarray having "aid": 1460. Please help me!!

Suresh Sharma
  • 186
  • 1
  • 5
  • 23

2 Answers2

1

It's fairly easy to test JSON response with REST Assured in built assertions but in your case you also require the index number of array that I'm not sure if it can find out.I think something along these lines will help

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


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

You can then use JSONArray to find out the index and also the corresponding elements.

rohit.jaryal
  • 320
  • 1
  • 8
0

Regarding the assertions you can use the RestAssured built-in GPath query syntax:

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"));
mle
  • 2,466
  • 1
  • 19
  • 25