How to test API Response by Rest Assured

  



What is an API Response?

  • When you send a request to an API (like GET, POST, PUT, DELETE), the server processes it and sends back a response.
  • An API Response generally contains:

Status Line → Includes status code (e.g., 200 OK, 404 Not Found, 500 Internal Server Error) and protocol version.

Response Headers → Provide metadata about the response (e.g., Content-Type, Server, Cache-Control).

Response Body → The actual data returned (in JSON, XML, or HTML). Example: user details, order status, product list, etc.


How to Test API Response using Rest Assured

Rest Assured is a Java-based library for testing REST APIs. It provides a fluent interface to validate API responses.


When testing an API response with Rest Assured, you typically check:

1. Response Status Code

  • Verify that the returned status code matches the expected one.
  • Example: A successful request should return 200, creating a resource might return 201, and invalid requests might return 400 or 404.


2. Response Headers
  • Validate that important headers are present and contain correct values.
  • Example: Content-Type should be application/json.

3. Response Body Content
  • Check if the data returned in the body is correct.
  • Example: For a user API, ensure the returned user’s id, name, or email matches what you expect.

4. Response Time / Performance

  • Measure how long the API took to respond.
  • Example: The response time should be within 2 seconds.


5. Schema Validation
  • Verify that the response structure matches the expected JSON or XML schema.
  • Example: Ensures fields like id, name, and email exist and are of the correct data type.

6. Error Handling
  • Check how the API behaves when invalid requests are sent.
  • Example: If you send a request with missing data, the API should return a proper error code and message.


In below example, we are going to test response of API.











This is the API we are going to test: 

https://reqres.in/api/users?page=2













Below are maven dependencies:

<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.9.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>




Java Code:

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.Test;

public class ResponseBodyContainsTest {

    @Test
    public void testResponseBodyContains() {
        // Set the base URI
        RestAssured.baseURI = "https://reqres.in";

        // Send GET request and capture response
        Response response = RestAssured
                .given()
                .when()
                .get("/api/users?page=2")
                .then()
                .statusCode(200) // Validate status code
                .extract()
                .response();

        // Convert response body to String
        String responseBody = response.asString();

        // Print for reference
        System.out.println("Response Body:\n" + responseBody);

        // Use contains() to validate specific strings
        Assert.assertTrue(responseBody.contains("George"), "Expected name not found in response");
        Assert.assertTrue(responseBody.contains("email"), "Expected 'email' field not found in response");
        Assert.assertTrue(responseBody.contains("janet.weaver@reqres.in") == false, "Unexpected email found in response");
    }
}



Code explanation:

1. Set the base URI
2. Send GET request and extract response
3. Convert response body to string type
4. Print response body
5. Use contains() to validate string


Important Points:
  • .contains() is case-sensitive.
  • You can test for presence/absence of any string that should/shouldn't be in the response.
  • Prefer this method for quick validations; for structured validations, use JSON path instead.


Suggested Posts:

1. Test Single Response Header in RestAssured
2. Test Digesh Auth in RestAssured
3. Test DELETE API in RestAssured
4. How to Test SOAP API by RestAssured
5. How to Test Basic Authentication in RestAssured