Introduction to REST Assured

Last Updated : 28 Jan, 2026

In today's rapidly evolving software development landscape, automating API testing is an essential task for ensuring the quality and reliability of web services. REST Assured is one of the most powerful tools available for this purpose. It is a Java-based library designed to simplify API automation testing by offering a clean and intuitive approach to writing test cases.

What is REST Assured?

REST Assured is a Java library designed to test and validate RESTful APIs. Unlike GUI-based tools like Postman, REST Assured integrates directly into your codebase, providing greater flexibility and control over your tests. It offers a Domain-Specific Language (DSL) that allows you to write tests for APIs in a simple, readable manner.

Key features of REST Assured include:

  • Simple, Intuitive Syntax: It uses the given, when, and then pattern for defining API tests.
  • Support for Various Authentication Mechanisms: Basic Authentication, OAuth, and more.
  • Built-in Validation: It allows you to validate status codes, response bodies, headers, cookies, and much more.
  • Seamless Integration: REST Assured integrates well with other tools like JUnit, TestNG, and CI/CD pipelines.
  • Open Source: REST Assured is free and open-source, providing significant cost savings over commercial tools.

Why Choose REST Assured?

1. Code-Based Flexibility

REST Assured offers code-based flexibility, which means you can include it directly in your test automation code. This has several advantages:

  • Better Version Control: Unlike GUI tools, your API tests are versioned alongside the rest of your code, making it easy to manage changes.
  • Reusability: You can reuse tests across different projects or environments without manual interventions.
  • Integration: It integrates easily with testing frameworks like JUnit, TestNG, and Selenium, allowing you to create complex, multi-layered testing strategies.

2. No Licensing Limitations

Unlike some commercial tools, REST Assured is open-source, meaning there are no subscription costs or licensing limitations. This makes it an ideal choice for teams of all sizes, from startups to large enterprises.

3. Supports Complex API Scenarios

REST Assured is highly capable of handling various complex API testing scenarios:

  • Request headers, cookies, and payloads
  • Schema validation
  • Performance testing
  • Data extraction from JSON and XML responses

Setting Up REST Assured

Before you can start testing with REST Assured, you need to set up your project with the required dependencies. Below are the steps to set up REST Assured in your Java project.

Prerequisites

  • Java: Ensure Java is installed on your system.
  • Maven or Gradle: REST Assured can be added as a dependency to your Maven or Gradle project.
  • IDE: Use any Java IDE (e.g., IntelliJ IDEA, Eclipse) for writing and running tests.

Adding REST Assured to Your Project

For Maven:

Add the following dependency to your pom.xml file:

XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.restassured.demo</groupId>
  <artifactId>rest-assured-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <!-- REST Assured Dependency -->
    <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.5.0</version>
    <scope>test</scope>
</dependency>


    <!-- JSON Path Dependency (Optional for JSON Parsing) -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>json-path</artifactId>
        <version>5.3.0</version>
    </dependency>

    <!-- TestNG Dependency for Test Cases -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.7.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

  
  
</project>

For Gradle:

For Gradle projects, add the following line to your build.gradle:

testImplementation 'io.rest-assured:rest-assured:5.0.0'

Writing Your First REST Assured Test

REST Assured uses a straightforward syntax following the given-when-then pattern:

  1. Given: Prepares the request.
  2. When: Executes the HTTP method (e.g., GET, POST).
  3. Then: Validates the response.

Example: Validate a GET Request

Here’s a basic test to validate a simple GET request:

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

import org.testng.annotations.Test;

public class APITest {
    @Test
    public void testGetUser() {
        given()
            .baseUri("https://jsonplaceholder.typicode.com")
        .when()
            .get("/users/1")
        .then()
            .statusCode(200)
            .body("name", equalTo("Leanne Graham"));
    }
}

Output:

Validate a GET Request
Validate a GET Request outputConclus

Explanation:

  • given(): Specifies the base URI of the API.
  • when(): Makes a GET request to /users/1.
  • then(): Verifies that the status code is 200 and the "name" field in the response body equals "Leanne Graham".

Advanced Features of REST Assured

1. Schema Validation

REST Assured can validate that the API response matches a predefined schema, ensuring that the response adheres to the expected structure.

.body(matchesJsonSchemaInClasspath("schema.json"));

This checks if the JSON response matches the schema defined in schema.json.

2. Authentication Support

REST Assured supports various authentication mechanisms. For example, to use Basic Authentication:

given()
.auth()
.basic("username", "password")
.when()
.get("/secure-endpoint")
.then()
.statusCode(200);

3. Extracting Data

You can extract data from the API response, such as specific fields in the response body:

String userName = 
given()
.baseUri("https://jsonplaceholder.typicode.com/")
.when()
.get("/users/1")
.then()
.extract()
.path("name");

This extracts the "name" from the JSON response.

4. Performance Testing

REST Assured also supports performance testing by validating the response time of an API:

.time(lessThan(2000L)); // Response should be under 2 seconds

REST Assured Workflow

  1. Prepare the Request: Set up the base URI, headers, and authentication.
  2. Send the Request: Use HTTP methods like GET, POST, PUT, DELETE.
  3. Receive the Response: Process the API response (in JSON, XML, etc.).
  4. Validate the Response: Validate the status code, response body, headers, and more.
  5. Extract Data (Optional): Extract specific information from the response for further validation.

Benefits of Using REST Assured

1. Ease of Use

REST Assured offers a clean, DSL-style syntax that is simple to learn and use, especially for developers familiar with Java.

2. Integration

REST Assured integrates seamlessly with testing frameworks like JUnit, TestNG, and Cucumber. It also works well with CI/CD pipelines, making it an essential tool in continuous testing environments.

3. Scalability

REST Assured is ideal for both small projects and large-scale API testing efforts. Its flexibility allows you to scale tests as your application grows.

4. Community Support

Being an open-source tool, REST Assured benefits from a large and active community. The tool has extensive documentation, forums, and resources to help developers resolve issues and improve their tests.

Comment

Explore