top of page

REST Assured Tutorial

API Automation Test Example

How To Write RESTful API Automation Test Using REST Assured ?

To write a sample REST API test, we will use following data :​

If we directly open the above url in the browser, we get this output :




To get the same output programmatically using Rest Assured, follow below steps :​
  • Use RestAssured class to generate a RequestSpecification for the URL :

  • Specify the HTTP method(GET method).

  • Send the request to the server.

  • Get the response back from the server.

  • Print the returned response.


Below is the complete code for above steps in two ways as:
  1. Using httpRequest.request(Method.GET, "")

  2. Using httpRequest.get("")

Using httpRequest.request(Method.GET, "")  :

package apiTesting;

import org.testng.annotations.Test;

import io.restassured.RestAssured;import io.restassured.http.Method;import io.restassured.specification.RequestSpecification;

public class RESTAPITestExample {


    @Test

   public void restAPIExample() {

       RestAssured.baseURI = "http://localhost:8080/msg/webapi/userProfiles";       

RequestSpecification httpRequest = RestAssured.given();

       io.restassured.response.Response response = httpRequest.request(Method.GET, "");

       System.out.println("Status Received ==>" + response.getStatusLine());     // returns http protocol version, status code, status code's string value

       System.out.println("Response ==>" + response.asString());

    }

}


Output :

[RemoteTestNG] detected TestNG version 6.14.3[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.Status Received ==>HTTP/1.1 200 [{"created":"2022-05-13T16:00:26.107Z[UTC]","firstName":"Bella","id":1,"lastName":"Gilbert","profileName":"Bella Profile"},{"created":"2022-05-13T16:00:26.107Z[UTC]","firstName":"Elena","id":2,"lastName":"Swan","profileName":"Elena Profile"},{"created":"2022-05-13T16:00:26.107Z[UTC]","firstName":"Caroline","id":3,"lastName":"Marshall","profileName":"Caroline Profile"},{"created":"2022-05-13T16:00:26.107Z[UTC]","firstName":"Sarah","id":4,"lastName":"Humpton","profileName":"Sarah Profile"}]PASSED: restAPIExample

===============================================    Default test    Tests run: 1, Failures: 0, Skips: 0===============================================

===============================================Default suiteTotal tests run: 1, Failures: 0, Skips: 0===============================================




Using httpRequest.get("")  :

package apiTesting;

import org.testng.annotations.Test;


import io.restassured.RestAssured;import io.restassured.specification.RequestSpecification;

public class RESTAPITestExample {

    @Test   public void restAPIExample() {

       RestAssured.baseURI = "http://localhost:8080/msg/webapi/userProfiles";

       RequestSpecification httpRequest = RestAssured.given();

       io.restassured.response.Response response = httpRequest.get("");

       System.out.println("Status Received ==>" + response.getStatusLine());        // returns http protocol version, status code, status code's string value

        System.out.println("Response ==>" + response.asString());

   }

}





Output :

[RemoteTestNG] detected TestNG version 6.14.3[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.Status Received ==>HTTP/1.1 200 [{"created":"2022-05-13T16:00:26.107Z[UTC]","firstName":"Bella","id":1,"lastName":"Gilbert","profileName":"Bella Profile"},{"created":"2022-05-13T16:00:26.107Z[UTC]","firstName":"Elena","id":2,"lastName":"Swan","profileName":"Elena Profile"},{"created":"2022-05-13T16:00:26.107Z[UTC]","firstName":"Caroline","id":3,"lastName":"Marshall","profileName":"Caroline Profile"},{"created":"2022-05-13T16:00:26.107Z[UTC]","firstName":"Sarah","id":4,"lastName":"Humpton","profileName":"Sarah Profile"}]PASSED: restAPIExample

===============================================    Default test    Tests run: 1, Failures: 0, Skips: 0===============================================

===============================================Default suiteTotal tests run: 1, Failures: 0, Skips: 0===============================================


In both programs, we can see the output is same, so we can use any of them.

Refer next page Validate Response Status
bottom of page