Accessing and testing contents of ArrayList<String> using GPath syntax

513 views
Skip to first unread message

Steve Webb

unread,
Jun 7, 2016, 7:29:58 AM6/7/16
to REST assured

I has a response body like this

enter code here

{
"applicationName": "Service MyService",
"someData": [
    {
        "name": "check1",
        "props": [
            "AAaa"
        ]
    },
    {
        "name": "check2",
        "props": [
            "BBbb",
            "CCcc"
        ]
    }
]
}

Now I can use the following code and the test passes.

        given().log().all()
        .accept(JSON).expect().statusCode(SC_OK)
    .when().log().all()
        .get(contextPath + "/test")
    .then().log().all()
        .body("someData.name",
            IsCollectionWithSize.hasSize(2))
        .body("someData.name",
            allOf(hasItems("check1", "check2")))
        .body("someData.findAll {it.name == 'check1'}.props", 
            IsCollectionWithSize.hasSize(1))
        .body("healthReports.findAll {it.name == 'check2'}.props", 
            IsCollectionWithSize.hasSize(2)));

However if I then attempt to check the values in the props field it fails I think because a ArrayList is returned and the matchers are checking on String.

        given().log().all()
        .accept(JSON).expect().statusCode(SC_OK)
    .when().log().all()
        .get(contextPath + "/test")
    .then().log().all()
        .body("someData.name",
            IsCollectionWithSize.hasSize(2))
        .body("healthReports.findAll {it.name == 'check1'}.props", 
                    IsCollectionContaining.hasItems(startsWith("AA")));

I'm not sure how from the findAll ...props I can check the contents of the ArrayList.

The error displayed is:

JSON path someData.findAll {it.name == 'check1'}.props doesn't match.
Expected: (a collection containing a string starting with "AA")
Actual: [[AAaa]]

Any idea's ?


Johan Haleby

unread,
Jun 7, 2016, 8:11:51 AM6/7/16
to rest-a...@googlegroups.com
That's probably because a list of lists are returned. First you find all elements in that contains name == 'check1' (which returns a list) and in this list you return the "props" which is also a list. Thus you get a nested list back. Add "flatten()" to your path ro return a flat list:

"healthReports.findAll {it.name == 'check1'}.props.flatten()"

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steve Webb

unread,
Jun 7, 2016, 8:59:25 AM6/7/16
to REST assured
Hi Johan thanks for the reply, I realised myself just before reading your reply! I changed the findAll to find and no list of list anymore.

Thanks for the quick response :)

Steve
Reply all
Reply to author
Forward
0 new messages