1

I have several JSON objects in response which contain certain id in each one of them.

{
"data": [{
        "id": 1,
        "status": {
            "id": 0
        }
    },
    {
        "id": 2,
        "status": {
            "id": 0
        }
    },
    {
        "id": 3,
        "status": {
            "id": 0
        }

    }
]

}

I need to test that every data.status.id has certain value:

    then().
        spec(basicResponse).
        body("data.status.id", equalTo(0));

But equalTo matcher comperes expected value with the whole list of the found ids at once. Not with each individual found id separately.

java.lang.AssertionError: 1 expectation failed.
JSON path data.status.id doesn't match.
Expected: 0
  Actual: [0, 0, 0]

How can I test it without extracting response data to List and then check it through foreach or something like that?

int statusNeeded = 0;
            List<int> idsList = given().
                     get(Endpoints.GetCampaigns).
                 then().
                     extract().body().jsonPath().getList("data.status.id");

    for (int statusId: idsList)
    if(!statusNeeded.equals(statusId)) Assert.fail("Some id is not " + statusNeeded);
Ip Man
  • 77
  • 10

2 Answers2

1

You can follow the below approach

String res = given().when().get("http://localhost:3000/posts/CtQy1bt").then().log().all().extract().response().asString();

JsonPath js = new JsonPath(res);

int Count = js.get("data.size()");

for (int i=0;i<Count;i++) {
    int value = js.get("data["+i+"].status.id");
    Assert.assertEquals(value, 0);
}

I have used JsonPath here, and used a for loop to iterate through the array for data[].status.id.

Assert it against an expected value which is 0 according to your requirement

If the value for status.id is not 0 then assertion will fail - Sample below

{
    "data": [
        {
            "id": 1,
            "status": {
                "id": 0
            }
        },
        {
            "id": 2,
            "status": {
                "id": 5
            }
        },
        {
            "id": 3,
            "status": {
                "id": 0
            }
        }
    ],
    "id": "nmBlS8t"
}
Exception in thread "main" java.lang.AssertionError: expected [0] but found [5]
    at org.testng.Assert.fail(Assert.java:97)
    at org.testng.Assert.assertEqualsImpl(Assert.java:136)
    at org.testng.Assert.assertEquals(Assert.java:118)
    at org.testng.Assert.assertEquals(Assert.java:839)
    at org.testng.Assert.assertEquals(Assert.java:849)
    at Stack5.main(Stack5.java:25)
Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
  • Yes, it worked out. But are you sure that such approach is faster to perform in comparison with approach I suggested on the bottom of my question via using foreach? – Ip Man Apr 21 '20 at 07:42
  • Do you seem to notice a difference ? This approach would iterate through key values but I don't suppose the concept of speed would much matter here – Wilfred Clement Apr 21 '20 at 10:18
0

Unfortunately, you do not describe further the structure of your JSON output. So, I'm forced to guess. Why don't you try?

$.data[0].status.id
nic
  • 119
  • 4
  • In your update, there is a mismatch. Looking at your JSON document, I think the output of your test should say `Actual: [0, 1, 0]`. Right? – nic Apr 20 '20 at 14:49
  • Yes, you are right. Sorry, I was inattentive and I forgot to edit some part of question after updating. – Ip Man Apr 20 '20 at 16:19