REST API Automation with REST Assured

 In today’s software world, APIs are the backbone of communication between services. To ensure APIs work as expected, automation testing becomes essential. REST Assured is a powerful Java-based library that simplifies REST API testing. It’s widely used for validating API responses, status codes, headers, and data.


πŸš€ What is REST Assured?

REST Assured is an open-source Java library for testing RESTful web services. It provides a fluent interface that makes writing API tests readable, concise, and efficient—perfect for developers and testers working with Java and Maven or Gradle projects.


✅ Why Use REST Assured?

Simple syntax, even for beginners

Supports all HTTP methods (GET, POST, PUT, DELETE, etc.)

Integrates easily with TestNG, JUnit, Maven, and CI/CD pipelines

Supports JSON/XML parsing, schema validation, and authentication


πŸ› ️ Basic Setup

Add Dependency (Maven):

<dependency>

    <groupId>io.rest-assured</groupId>

    <artifactId>rest-assured</artifactId>

    <version>5.3.0</version>

    <scope>test</scope>

</dependency>

Add Static Imports (Java):

import static io.restassured.RestAssured.*;

import static org.hamcrest.Matchers.*;


πŸ” Sample Test: GET Request

import io.restassured.RestAssured;

import org.testng.annotations.Test;

public class APITest {

    @Test

    public void getUserDetails() {

        RestAssured

            .given()

                .baseUri("https://reqres.in/api")

                .basePath("/users/2")

            .when()

                .get()

            .then()

                .statusCode(200)

                .body("data.id", equalTo(2))

                .body("data.email", containsString("@reqres.in"));

    }

}


πŸ“€ Sample Test: POST Request

public void createUser() {

    String requestBody = "{ \"name\": \"John\", \"job\": \"QA\" }";

    RestAssured

        .given()

            .header("Content-Type", "application/json")

            .body(requestBody)

        .when()

            .post("https://reqres.in/api/users")

        .then()

            .statusCode(201)

            .body("name", equalTo("John"));

}


πŸ” Authentication Support

REST Assured supports:

Basic Auth: .auth().basic("user", "pass")

OAuth2: .auth().oauth2("token")

Preemptive Auth: .auth().preemptive().basic("user", "pass")


πŸ”§ Advanced Features

  • JSON Schema validation
  • File uploads
  • Logging request/response
  • Parameterized requests


🧠 Final Thoughts

REST Assured makes API automation in Java simple and powerful. With clear syntax and strong integration capabilities, it’s ideal for automating functional and regression testing for RESTful APIs. Combined with frameworks like TestNG or JUnit, it helps ensure your backend services are always reliable.

Learn Testing toolsTraining in Hyderabad

Read More:

Introduction to LoadRunner for Load Testing

End-to-End Testing with Playwright

Zephyr for Test Management in JIRA

Comparing Selenium, Cypress, and Playwright

Visit our IHub Talent Training Institute

Get Direction

Comments

Popular posts from this blog

Tosca Installation and Environment Setup

Understanding Tosca TestCases and Modules

How Tosca Handles TestData Parameterization