API Automation with Rest Assured

Why Rest Assured For API Testing?

Rest assured tutorial 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 – Rest Assured Tutorial for API Testing

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

Pre-Requisites:

  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

Steps:

Step 1: Create a Maven Project

  1. At first, click on File and Again click on New
  2. Secondly go to Project  and select Maven Project
  3. Lastly 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 ->
<!– https://mvnrepository.com/artifact/org.testng/testng –>
<dependency>
    <groupId>org.testng</groupId>

    <artifactId>testng</artifactId>

    <version>6.14.3</version>

    <scope>test</scope>

</dependency>

<!- Rest Assured Dependency ->
<!– https://mvnrepository.com/artifact/io.rest-assured/rest-assured –>

<dependency>

    <groupId>io.rest-assured</groupId>

    <artifactId>rest-assured</artifactId>

    <version>3.3.0</version>

    <scope>test</scope>

</dependency>
packages

Gather API Details for Test – Step 3

BaseURI : https://reqres.in/

Method : GET

END Point : api/users/2

Expected ResponseCode : 200

JSON :

{

    “data”: {

        “id”: 2,

        “email”: “janet.weaver@reqres.in”,

        “first_name”: “Janet”,

        “last_name”: “Weaver”,

        “avatar”: “https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg”

    }

}

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 {

       @Test

       public void testSingleUser() {

       // Store Base URI
       // Line 1
       RestAssured.baseURI = “https://reqres.in/”;
       
        // 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
emailable-report.html

Code Explanation:

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

RestAssured.baseURI = “https://reqres.in/”;

Line 2: Specify String body (JSON or XML) that is the 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);

To this end, hope above article helps you to create first Rest Assured Test. Additionally, you may feel free to contact us or mail us at query@thoughtcoders.com or call us on 9555902032. Join us on Facebook, Twitter, LinkedIn or Instagram!