简体   繁体   中英

Rest-Assured unable to validate JSON array response

I'm trying to validate that a JSON array contains a certain value. Using Rest-Assured with a hamcrest matchers import in Java. This is the JSON that I'm validating;

{
    "graph": {
        "groupedResultColumns": [
            "Task_Status",
            "Task_TimeSpent"
        ]
    }
}

After reading about rest assured and hamcrest matchers, this is the code that I am trying at the moment;

{
    SerenityRest.then()
      .body(containsString("groupedResultColumns"))
      .assertThat().body("groupedResultColumns", (hasItems("Task_TimeSpent")));
}

This is the error I'm getting;

JSON path groupedResultColumns doesn't match.
Expected: (a collection containing "Task_TimeSpent")
  Actual: null

Any help or advice is appreciated, Thanks!

You need to specify the json path to the collection. The "groupedResultColumns" is inside "graph" so your path to the body would be graph.groupedResultColumns .

Your code would be something like this:

{
    SerenityRest.then()
      .body(containsString("groupedResultColumns"))
      .assertThat().body("graph.groupedResultColumns", (hasItems("Task_TimeSpent")));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM