How to assert on JSON response Data in RestAssured
- Dev Raj Sinha
- Jun 17, 2023
- 2 min read
Asserting on response data in RestAssured allows you to validate that the API response contains the expected values. RestAssured provides a range of assertion methods that make it easy to perform such validations. Here's a step-by-step guide on how to assert on response data using RestAssured:
Step 1: Make an API Request
First, send an API request using RestAssured. You can use 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: Perform Assertions on Response Data
RestAssured provides various assertion methods to validate response data. You can assert on the response body, status code, headers, or specific fields within the response. Here are a few examples:
a. Asserting Response Body:
To assert on the response body, you can use the `assertThat()` method and provide a Hamcrest matcher to compare the expected and actual values. Here's an example:
import static org.hamcrest.Matchers.equalTo;
RestAssured.get("https://api.example.com/user/1")
.then()
.assertThat()
.body("name", equalTo("John Doe"))
.body("age", equalTo(30));
In this code snippet, we assert that the response body has a "name" field with the value "John Doe" and an "age" field with the value 30.
b. Asserting Status Code:
To assert on the status code, you can use the `assertThat()` method along with the `statusCode()` method. Here's an example:
RestAssured.get("https://api.example.com/user/1")
.then()
.assertThat()
.statusCode(200);
In this example, we assert that the status code of the response is 200 (OK).
c. Asserting Headers:
To assert on response headers, you can use the `assertThat()` method along with the `header()` method. Here's an example:
RestAssured.get("https://api.example.com/user/1")
.then()
.assertThat()
.header("Content-Type", "application/json")
.header("Cache-Control", "no-cache");
In this code snippet, we assert that the "Content-Type" header is set to "application/json" and the "Cache-Control" header is set to "no-cache".
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 response data in RestAssured allows you to validate the correctness of API responses during testing. By leveraging the assertion methods provided by RestAssured, you can ensure that the response data meets your expected criteria, whether it's the response body, status code, headers, or specific fields within the response. These assertions play a crucial role in verifying the functionality and reliability of your APIs.
Comments