Rest Assured Tutorial For API Testing

Why Rest Assured For API Testing?

As Webservices are highly preferred in complex Application Development, API Testing is very demanding these days. API Testing is mostly pre-development testing so it’s early testing and it’s a better approach for testing because it helps to find an early defect, quicker( as no UI involves) and automatable.

Rest Assured Configuration Step By Step

So here is the step by step Rest Assured configuration and first Test. To start API Automation Testing we need the following prerequisite.


  1. Windows/Linux/Mac Machine
  2. JDK/Open JDK Installed
  3. Maven Installed
  4. Eclipse (or IntelliJ)
  5. TestNG Plugin installed in Eclipse.
  6. API Details and Test Cases


Step 1: Create a Maven Project

  1. Click on File and Again click on New
  2. Go to Project  and select Maven Project
  3. Add Project artifacts as per below snapshot
New Project
New Project Parameters

Step 2. Open POM.xml and add Rest Assured & TestNG Dependency

<!- Testng Dependency ->
<!– –>





<!- Rest Assured Dependency ->
<!– –>







Step 3: Gather API Details for Test

BaseURI :

Method : GET

END Point : api/users/2

Expected ResponseCode : 200



    “data”: {

        “id”: 2,

        “email”: “”,

        “first_name”: “Janet”,

        “last_name”: “Weaver”,

        “avatar”: “”



Step 4: Write below code

package apiTest;

import org.testng.Assert;

import org.testng.annotations.Test;

import io.restassured.RestAssured;

import io.restassured.http.Method;

import io.restassured.response.Response;

import io.restassured.specification.RequestSpecification;

public class APIResponseTest {


       public void testSingleUser() {

       // Store Base URI
       // Line 1
       RestAssured.baseURI = “”;
        // Make Request Specification using above give BASE URI
       // Line 2
       RequestSpecification httpRequest = RestAssured.given();

       // Response varible used to store Server Response
       // Line 3
       Response response = httpRequest.request(Method.GET, “api/users/2”);

       // Store Response Body as String
       // Line 4
       String responseBody = response.getBody().asString();
       // Store response Code as integer
       int responseCode= response.getStatusCode();

       //validate responseCode
       // Line 5
       Assert.assertEquals(200, responseCode);



Step 5: Right-click on Project, click, refresh and go to the test-output folder and verify report “emailable-report.html”

Rest Assured - Step by Step Tutorial

Code Explanation:

Line 1: StoreBase URI which will be sent to the server as Request.

RestAssured.baseURI = “”;

Line 2: Specify String body (JSON or XML) that will be sent request. This will work with POST and PUT methods only.

RequestSpecification httpRequest = RestAssured.given();

Line 3: This is request sent to server in given format and store server response in Response object.

Response response = httpRequest.request(Method.GET, “api/users/2”);

Line 4:  Get  server response code and store into responseCode

int responseCode= response.getStatusCode();

Line 5:  Tesng Method to assert whether server response is 200 or not

Assert.assertEquals(200, responseCode);

Hope above article helps you to create first Rest Assured Test. Feel free to contact us on or call us on 9555902032.


Subscribe to our Newsletter:

Open chat
Feel free to contact