rest-assuredrest-assured-jsonpath

Rest Assured response body validation


I have a question related to response body validation in Rest Assured. Let's suppose, I have such a response body as json.

{
    "store": {
        "books_count":3,
        "books": [
            {
                "genre": "fiction",
                "author": {
                    "name": "William",
                    "lastName" : "Evans"
                }
            },
            {
                "genre": "kids",
                "author": {
                    "name": "Eric",
                    "lastName" : "Carle"
                }
            },
            {
                "genre": "science",
                "author": {
                    "name": "Ronald",
                    "lastName" : "Forks"
                }
            }
        ]
    }
}

I need to check:

  1. if books section contains a kids genre
  2. if a book of kids genre has autor name as Eric

the first assertion can be like:

response.body("store.books.genre", hasItem("kids);

But then I need to check if its author's name is William. Is there any way to check it using jsonPath?

I assume, that I can do deserialization (e.x. List<Book.class>, and then get an Author.class etc...), but is there any opportunity to check it with RestAssured. Thanks in advance!


Solution

  • if a book of kids genre has autor name as Eric

    You can leverage the GPath to extract value by condition

    .body("store.books.find {it.genre == 'kids'}.author.name", is("Eric");