REST Assured API Test Automation

loopback_logo

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”);
    login.setActionName(“login”);
    login.setEmail(“testingalertindia@gmail.com”);
    login.setPassword(“123456”);
    login.setMedium(“MANUAL”);
    login.setAppKey(“myAppKey”);
  • setContentType() – Pass the “application/json“, “application/xml” or “text/html” etc. headers to setContenType() method

Prerequisites:-

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:-

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 :-

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

Conclusion:-

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

10 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”: “kkashyap1707@gmail.com“,
      “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) {
      this.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

      this.setApiVersion(“1.0”);
      this.setActionName(“login”);
      this.setEmail(“testingalertindia@gmail.com”);
      this.setPassword(“123456”);
      this.setMedium(“MANUAL”);
      this.setAppKey(“myAppKey”);

      }

  • 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

Leave a Reply

%d bloggers like this: