How to Test GET API by Rest Assured

 



What is Rest Assured?

  • Rest Assured is a Java-based library used for testing RESTful APIs.
  • It provides a domain-specific language (DSL) that makes writing HTTP request tests simple and readable.
  • You can use it to send requests like GET, POST, PUT, DELETE, etc., and then validate responses (status code, headers, body, etc.).
  • It integrates well with JUnit, TestNG, Maven, and CI/CD tools.

To test a GET API using Rest Assured in Java, you can follow the steps below. We'll walk through the process and test the endpoint:









API Endpointhttps://reqres.in/api/users

This API returns paginated user data in JSON format.















Basic Steps to Test GET API

  • Add Rest Assured dependency in your pom.xml (if using Maven).
  • Set the Base URI.
  • Send the GET request.
  • Validate the Response (status code, headers, body).
  • Log response (optional but useful for debugging).


Maven Dependency (pom.xml):

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



Java Code to Test GET API

import io.restassured.RestAssured;
import io.restassured.response.Response;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class GetUserApiTest {

    public static void main(String[] args) {

        // Step 1: Set Base URI
        RestAssured.baseURI = "https://reqres.in";

        // Step 2: Send GET request and validate
        given()
            .log().all()  // Logs the request details
        .when()
            .get("/api/users?page=2")  // Endpoint
        .then()
            .log().all()  // Logs the response details
            .assertThat()
            .statusCode(200)  // Validate HTTP Status code
            .body("data", notNullValue())  // Validate response body contains 'data'
            .body("data.size()", greaterThan(0))  // Validate non-empty data
            .body("data[0].id", notNullValue());  // Validate user id present
    }
}



Explanation of Code:

LinePurpose
baseURISets the base URL for the request.
given()Prepares request specification.
log().all()Logs all request and response data for visibility.
get()Triggers the GET request to the specified endpoint.
then()Begins response verification.
statusCode(200)Verifies the status code is 200 (OK).
body(...)Verifies the presence and contents of specific JSON fields.



Response Validation:

Below is the screenshot of code which is validating GET API response.







In above screenshot, data is json array, and code is validating that data contains any values or not or we can say it is null or not. In the second line we are verifying size of array data is greater than 0 or not and in third line we are verifying, first element of data array that is id, is null or contains any value.


Sample JSON Output from API

{
  "page": 2,
  "per_page": 6,
  "data": [
    {
      "id": 7,
      "email": "michael.lawson@reqres.in",
      "first_name": "Michael",
      "last_name": "Lawson",
      ...
    }
  ]
}


Suggested Posts:

1. Validate Keys in API in RestAssured
2. How to extract Response in Rest Assured by JSONPath
3. Test PUT API in RestAssured
4. Test DELETE API in RestAssured
5. How to Test Basic Authentication in RestAssured

First Rest Assured Code to Test API

 



To test any API using Rest Assured, you need to follow these steps:

1. Setup

Before writing any Rest Assured test, we need to set up:
  • A Java project (Maven or Gradle is preferred).
  • Add the Rest Assured dependency in pom.xml or build.gradle.
  • Optionally add TestNG or JUnit as the testing framework.

2. Import Required Classes
  • In a Rest Assured test, you typically import:
  • Rest Assured methods for sending requests.
  • Assertions from TestNG/JUnit to validate the response.

3. Define the Base URI
  • You specify the API endpoint you want to test.
  • Example: If you are testing a sample API like https://reqres.in/api/users, this becomes your base URI.

4. Send a Request

You make a request to the API:
  • GET request to fetch data.
  • POST request to send data.
  • PUT/PATCH request to update data.
  • DELETE request to remove data.
The request can include headers, query parameters, or body depending on the API requirements.


5. Capture the Response

When the API responds, Rest Assured stores details such as:
  • Status code (200, 201, 400, 404, etc.)
  • Response body (JSON, XML, text, etc.)
  • Headers (content-type, server info, etc.)
  • Response time

6. Validate the Response

Assertions are applied to verify if the API behaves as expected. Typical checks are:
  • Status code is correct (e.g., 200 for success).
  • Response body contains expected values (e.g., user name, ID).
  • Headers are correct (e.g., content-type is application/json).
  • Response time is within acceptable limits.

7. Organize into a Test Case

The above steps (setup → request → response → validation) are wrapped inside a test method using TestNG or JUnit. This test can then be executed like any other automated test case.

Below we are automating GET API from reqres.in 

























Add 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> <!-- Latest version -->
    <scope>test</scope>
  </dependency>
</dependencies>



Take a simple example of API: 
https://reqres.in/api/users


Rest Assured Code:

To get Status code of given API:

	@Test
	public void test() {
		
		Response response = RestAssured.get("https://reqres.in/api/users?page=2");
		System.out.println(response.statusCode());
		
	}



To log all response on console:

	@Test
	public void test1() {
		RestAssured.baseURI = "https://reqres.in/api/";
		RestAssured.given().get("users?page=2").then().statusCode(200).log().all();
	}



Code Explanation:


LinePurpose
baseURISets the base URL.
given()Prepares request specification.
when().get()Triggers GET request.
then()Begins response verification.
log().all()Logs complete request/response.
statusCode(200)Asserts HTTP status.


Suggested Posts:

1. Execute Playwright Script on Chrome Browser
2. How to extract Response in Rest Assured by JSONPath
3. Test PUT API in RestAssured
4. Test DELETE API in RestAssured
5. How to Test Status Line of API by RestAssured