REST Assured API Test Automation


What is REST Assured?

REST Assured is a Java library for validation of REST web services. It offers a friendly DSL (Domain specific Languages) that describes a connection to an HTTP endpoint and with their expected results. In REST Assured testing, we send a network request to our application, get a response back and compare it against a predetermined or expected result. Testing HTTP error codes becomes very useful when we want to make sure that our application behaves correctly, even when the input data is wrong.

REST Assured API Test Automation:-

Here we will automate REST API’s Post method using Rest-Assured library. We use Login create a post request. Use the following parameters in code.

  • APIUrl – Set URL variable of the Rest API
  • APIBody – Set APIBody variable using setter with body of the Rest API parameters e.g.  login.setApiVersion(“1.0”);
  • setContentType() – Pass the “application/json“, “application/xml” or “text/html” etc. headers to setContenType() method


First of all we fulfill below mentioned prerequisites to Automate an API using REST Assured:-

  • Java should be installed Link
  • Rest Assured Java Jar / Maven Dependency
  • A sample Java project  having HTTP/REST/JSON API implementations.

Rest Assured Maven Dependency:-

Rest Assured Maven Dependency Download Link

Sample Rest Assured Example:-

import static com.jayway.restassured.RestAssured.given;
import org.junit.Test;
public class HelloWorldRestAssured
public void makeSureThatGoogleIsUp()

Given When Then Structure:-

Rest-Assured is using given/when/then notation.When you create BDD features, the majority of its scenarios will be made up of three main Gherkin keywords: Given, When and Then. Each one has a specific function:-

  • Given= a context or Getting a system in a particular state
  • When= something happens or Poke it
  • Then=we expect some outcome or examine the new state

Test Case :-

Automate Login API and validate that the user logged in successfully having “Login successfully” returned JSON response message.

Test Script :-

package com.testingalert;
import com.Input.InputLogin;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class LoginRest
public static void main(String args[])
InputLogin login = new InputLogin();
//Set JSON Input Parameters
Gson gson = new Gson();
System.out.println("Input JSON Request :: "+ gson.toJson(login)); //Return Input JSON Request
Response res = RestAssured.given().contentType("application/json").body(login).when().post("");
System.out.println(" Output Response :: "+res.andReturn().asString()); //Return Output JSON Response
//Test Step Validation
if(res.path("message").equals("Login successfully"))
System.out.println("Response Time ->"+res.time()+"::::::"+ res.path("message"));
System.out.println("Failure Message :: " +res.path("message"));
catch(Exception e)
System.out.println( e.getMessage());

view raw
hosted with ❤ by GitHub

Google, performs a GET call and makes sure that HTTP code 200 or success HTTP response is returned.

Gson is automatically used by REST Assured for JSON (de)serialization, as we will see in the examples

Validation :-

  • Verify the http response status returned.
  • Verify that the response was successful  which returned correct JSON.

Response Assertions:-

REST Assured also handles assertion within its library, the parsing of responses.

In the above example, we validated the output response message and generate response time. It provides various constructs for implementing assertions on cookies, response headers and response body. For doing assertions on response body it provides JsonPath for assertions on JSON responses and XmlPath for assertions on XML responses.

Other Rest Java Libraries:-

  • Jersey
  • Jackson
  • Spring REST web service using MVC
  • Axis
  • Jersey
  • Restlet
  • EasyRest


As a result we have automated tests for the REST API of a sample login application using the Java library REST Assured.

11 thoughts on “REST Assured API Test Automation

    • Hello Vinod,
      First of all the raw payload you provided is not in JSON format. It should be like :-

      “status”: 1,
      “message”: “Login successfully”,
      “data”: {
      “email”: ““,
      “password”: “123456”,
      “medium”: “manualLogin”,
      “appkey”: 8,
      “actionName”: “login”,

      “code”: 10001

      Below is the correct way to post a request with raw payload :-

      private String apiVersion;
      private String actionName;
      private String email;
      private String password;
      private String medium;
      private String appKey;

      public String getApiVersion() {
      return apiVersion;
      public void setApiVersion(String apiVersion) {
      this.apiVersion = apiVersion;
      public String getActionName() {
      return actionName;
      public void setActionName(String actionName) {
      this.actionName = actionName;
      public String getEmail() {
      return email;
      public void setEmail(String email) { = email;
      public String getPassword() {
      return password;
      public void setPassword(String password) {
      this.password = password;
      public String getMedium() {
      return medium;
      public void setMedium(String medium) {
      this.medium = medium;
      public String getAppKey() {
      return appKey;
      public void setAppKey(String appKey) {
      this.appKey = appKey;
      public void setFields() {
      //Call setters



  • I have 2 question before using Rest Assured:
    1. Can we create test suite?
    2. Can we run the tests in parallel?

  • HI, Very useful info.Thanks for such a useful post. How to generate the token what is the expiry time of token?

  • To generate token you have to capture the login api response then convert it into models and then fetch the Access token from it

    Next is expiry time of token : it depends on the behavior of your application that on which situation it expires. It differs on the behaviour of the applications

  • HI , Thanks for the reply. Could you please provide example of fetching token from the login response. THanks.

  • Hi Sir,

    When I was run the login test script, it gives me some error in InputLogin login = new InputLogin();, i have already import com.Input.InputLogin

Leave a Reply

%d bloggers like this: