#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
API Testing

Introduction to Restassured

What is Rest Assured?

Rest Assured is an open-source Java library designed specifically for testing RESTful APIs, using a readable, fluent syntax often described as "Given-When-Then."

Basic Example

given()
  .baseUri("https://api.example.com")
.when()
  .get("/users/1")
.then()
  .statusCode(200)
  .body("name", equalTo("Asha"));

Why It's Popular

Rest Assured integrates smoothly with Java testing frameworks like TestNG and JUnit, and its syntax closely mirrors how testers naturally describe API tests (given some setup, when a request is sent, then verify the response).

Adding Rest Assured to a Maven Project

Rest Assured is distributed through Maven Central under the group id io.rest-assured. Add it with test scope next to your test runner. Check Maven Central for the newest stable versions; the numbers here are only examples.

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

<!-- Optional: validate responses against a JSON Schema -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>5.5.0</version>
    <scope>test</scope>
</dependency>

Put your API test classes under src/test/java, keep shared settings such as the base URL in a base class or properties file, and run everything with mvn test so the same tests work on a CI server.

What You Can Verify With It

Rest Assured covers most of what an API tester checks day to day:

  • Status codes such as 200, 201, 400, 401 and 404.
  • Headers and cookies, for example Content-Type or caching headers.
  • Response body fields in JSON or XML, using path expressions and Hamcrest matchers.
  • Response time, as a basic guard against slow endpoints.
  • Response structure, by matching the body against a JSON Schema file.
  • Authentication flows, including basic auth, bearer tokens and OAuth 2 headers.

Worked example: token, then protected call

Many APIs need a token before other endpoints can be called. The test below logs in, keeps the token, and uses it in a second request, then validates the response shape against a schema stored in src/test/resources.

import static io.restassured.RestAssured.*;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;

import io.restassured.path.json.JsonPath;

String loginJson = given()
        .baseUri("https://api.example.com")
        .contentType("application/json")
        .body("{\"email\":\"qa@example.com\",\"password\":\"secret\"}")
    .when()
        .post("/auth/login")
    .then()
        .statusCode(200)
        .extract().asString();

String token = new JsonPath(loginJson).getString("token");

given()
    .baseUri("https://api.example.com")
    .auth().oauth2(token)                 // sends Authorization: Bearer <token>
.when()
    .get("/orders")
.then()
    .statusCode(200)
    .body(matchesJsonSchemaInClasspath("orders-schema.json"));

In a real project, read credentials from environment variables or a secrets store rather than writing them in the test.

Rest Assured Compared With Other API Tools

ToolStrengthGood for
PostmanVisual, quick to explore endpointsManual checks, sharing collections
Rest AssuredPlain Java code, version-controlled, CI-friendlyAutomated regression in Java teams
KarateFeature-file syntax with built-in assertionsTeams wanting less Java code
Python requests with pytestLightweight scriptingPython-based test stacks

A common path is to explore an endpoint in Postman first, then turn the stable checks into Rest Assured tests that run on every build.

Practical Tips Before You Write Many Tests

  • Test negative cases too: missing fields, wrong types, expired tokens and unknown ids.
  • Keep each test independent by creating its own data and cleaning it up.
  • Store request bodies as POJOs or files once payloads grow beyond a few fields.
  • Point tests at a dedicated test environment, never at production data.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course