Search code examples
intellij-ideagetrest-assuredrest-assured-jsonpath

I wanted to verify a particular field is present & print on console using RESTAssured API Automation testing


I wanted to verify a particular field is present & print on console using RESTAssured API Automation testing in IntelliJ IDea. The field is in array and have multiple values.

  • I wanted to print the field in console as well.

  • Field name is annualBasePay and its value is 124917.

I need to verify only the field annualBasePay

[
  {
    "worker": {
      "wID": "137cf520",
      "employeeID": "T19"
    },
    "workerDescription": "Tim Moore",
    "workerId": "T19",
    "userId": "T19",
    "workerType": "Employee",
    "jobRelatedInfoType": {
      "positionTitle": "Systems Architect 5",
      "manager": {
        "wID": "1b1696eabe",
        "employeeID": "T823"
      },
      "adjustedServiceDate": "1976-01-16",
      "annualBasePay": "124917",
      "annualBaseCurrency": null
    }
  }
]

Solution

  • You can choose one of 2 ways:

    given()
      ...
      .then()
      .body("jobRelatedInfoType.annualBasePay", hasItems("124917"));
    

    or

    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.Matchers.hasItems;
    
    Response res = given()...;
    List<String> basePays = JsonPath.with(res.asString()).get("jobRelatedInfoType.annualBasePay");
    assertThat(basePays, hasItems("124917"));