How to Assert on multilayered Response Data in RestAssured
- Dev Raj Sinha
- Jun 17, 2023
- 3 min read
Asserting on multilayer response data in RestAssured involves validating specific fields or values nested within the response body. RestAssured provides flexible options to navigate through complex JSON or XML structures and assert on nested elements. Here's a step-by-step guide on how to assert on multilayer response data in RestAssured:
Step 1: Make an API Request Start by sending an API request using RestAssured. Choose the appropriate HTTP method (get(), post(), put(), delete(), etc.) based on your testing scenario. Here's an example of sending a GET request:
import io.restassured.RestAssured;
import io.restassured.response.Response;
Response response = RestAssured.get("https://api.example.com/user/1");
In this example, we send a GET request to retrieve user data from the API, and the response is stored in the response variable.
Step 2: Assert on Multilayer Response Data RestAssured provides various methods to navigate through and assert on multilayer response data. Here are a few examples:
a. Using JSONPath: If the response body is in JSON format, you can use JSONPath expressions to navigate and assert on nested elements. RestAssured provides the JsonPath class to parse the response body and extract values for assertions. Here's an example:
import io.restassured.path.json.JsonPath;
Response response = RestAssured.get("https://api.example.com/user/1");
JsonPath jsonPath = response.jsonPath();
String userName = jsonPath.getString("data.name");
int userAge = jsonPath.getInt("data.age");
assertThat(userName).isEqualTo("John Doe");
assertThat(userAge).isGreaterThan(18);
In this code snippet, we use JSONPath expressions to extract the "name" and "age" fields nested within the "data" object in the response body. We then assert on these values using standard assertion methods.
b. Using XPath: If the response body is in XML format, you can use XPath expressions to navigate and assert on nested elements. RestAssured provides the XmlPath class to parse the response body and extract values for assertions. Here's an example:
import io.restassured.path.xml.XmlPath;
Response response = RestAssured.get("https://api.example.com/user/1");
XmlPath xmlPath = response.xmlPath();
String userName = xmlPath.getString("data.name");
int userAge = xmlPath.getInt("data.age");
assertThat(userName).isEqualTo("John Doe");
assertThat(userAge).isGreaterThan(18);
Asserting on multilayer response data in RestAssured involves validating specific fields or values nested within the response body. RestAssured provides flexible options to navigate through complex JSON or XML structures and assert on nested elements. Here's a step-by-step guide on how to assert on multilayer response data in RestAssured:
Step 1: Make an API Request Start by sending an API request using RestAssured. Choose the appropriate HTTP method (get(), post(), put(), delete(), etc.) based on your testing scenario. Here's an example of sending a GET request:
javaCopy code
import io.restassured.RestAssured;
import io.restassured.response.Response;
Response response = RestAssured.get("https://api.example.com/user/1");
In this example, we send a GET request to retrieve user data from the API, and the response is stored in the response variable.
Step 2: Assert on Multilayer Response Data RestAssured provides various methods to navigate through and assert on multilayer response data. Here are a few examples:
a. Using JSONPath: If the response body is in JSON format, you can use JSONPath expressions to navigate and assert on nested elements. RestAssured provides the JsonPath class to parse the response body and extract values for assertions. Here's an example:
javaCopy code
import io.restassured.path.json.JsonPath;
Response response = RestAssured.get("https://api.example.com/user/1");
JsonPath jsonPath = response.jsonPath();
String userName = jsonPath.getString("data.name");
int userAge = jsonPath.getInt("data.age");
assertThat(userName).isEqualTo("John Doe");
assertThat(userAge).isGreaterThan(18);
In this code snippet, we use JSONPath expressions to extract the "name" and "age" fields nested within the "data" object in the response body. We then assert on these values using standard assertion methods.
b. Using XPath: If the response body is in XML format, you can use XPath expressions to navigate and assert on nested elements. RestAssured provides the XmlPath class to parse the response body and extract values for assertions. Here's an example:
javaCopy code
import io.restassured.path.xml.XmlPath;
Response response = RestAssured.get("https://api.example.com/user/1");
XmlPath xmlPath = response.xmlPath();
String userName = xmlPath.getString("data.name");
int userAge = xmlPath.getInt("data.age");
assertThat(userName).isEqualTo("John Doe");
assertThat(userAge).isGreaterThan(18);
In this code snippet, we use XPath expressions to extract the "name" and "age" elements nested within the "data" element in the response body. We then assert on these values using standard assertion methods.
Step 3: Handling Assertion Failures When an assertion fails, RestAssured throws an exception, indicating the reason for the failure. You can handle these exceptions using standard Java exception handling techniques, such as try-catch blocks or letting the exception propagate.
Conclusion: Asserting on multilayer response data in RestAssured allows you to validate specific fields or values nested within the response body, whether it's a complex JSON structure or an XML document. By using JSONPath or XPath expressions along with RestAssured's JsonPath or XmlPath classes, you can easily navigate through the response body and assert on the desired elements. These assertions enable you to ensure the correctness of your API responses, especially when dealing with multilayer data structures.
Comments