Unlocking the Power of API Automation with Rest Assured and TestNG

Unlocking the Power of API Automation with Rest Assured and TestNG person Vishnu Hari | Published: Tue Sep 2023

Rest Assured is a powerful tool for automating and testing APIs, especially when working with RESTful web services. It simplifies the process of sending HTTP requests and validating the responses, making it an invaluable asset for developers and testers. With Rest Assured, you can easily perform tasks like sending GET, POST, PUT, or DELETE requests and then verifying the results. Its fluent and intuitive syntax allows you to write clear and concise tests, ensuring your APIs function correctly and efficiently. Whether you're new to API testing or an experienced developer, Rest Assured is a fantastic addition to your toolkit for robust and reliable API automation.

Before we start the tutorial, just to clarify, I'm using a Windows 10 computer with OpenJDK, Eclipse IDE and testNG plugin already installed. Once you've got everything ready, you're all prepared to begin crafting your Maven Project Rest Assured. and TestNG.

Setup Rest Assured

1. Start by creating a Maven Project in your preferred IDE; we're using Eclipse, but the structure will be similar in any IDE.

2. Now, open your POM.xml file and add the following dependency.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>RestAssured</groupId>
  <artifactId>RestAssured</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestAssured</name>
  
    <dependencies>
 
  <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>4.1.1</version>
  </dependency>
 
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.8.0</version>
    <scope>test</scope>
</dependency>
 
<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy</artifactId>
    <version>3.0.8</version>
</dependency>
    
</dependencies>
 
</project>
 
For our testing purposes, we'll be using TinkerBridge Hosted Fake REST-API Service.

Get Method with Rest Assured

In this example, we're making a GET request to the "https://www.tinkerbridge.com/api/user/1" endpoint, which returns information about a user. We then retrieve the response body as a string and print it, along with the status code of the response.

package testApi;
 
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
 
 
public class api {
 
    @BeforeMethod
    public void setUp() {
    // Specify the base URI
    RestAssured.baseURI = "https://www.tinkerbridge.com/api";
    }
    
@Test
public void GetUser() { 
 
        
        // Send a GET request to a specific endpoint
        Response response = RestAssured.get("/user/1");
 
        // Get the response body as a string
        String responseBody = response.getBody().asString();
 
        // Print the response body
        System.out.println("Response Body: " + responseBody);
 
        // Get the status code
        int statusCode = response.getStatusCode();
 
        // Print the status code
        System.out.println("Status Code: " + statusCode);
     
}
 
}

Output

Post Method with Rest Assured

In this example, we're making a POST request to the "https://www.tinkerbridge.com/api/add-user" endpoint with a JSON request body. We then retrieve the response body as a string and print it, along with the status code of the response.

 @Test
 public void SetUser() { 
 
 
// Define the request body as a JSON object
String requestBody = "{\"name\": \"Vishnu Hari\", \"email\": \"vishnu@example.com\"}";
 
// Send a POST request to a specific endpoint with the request body
Response response = RestAssured
.given()
.contentType(ContentType.JSON)
.body(requestBody)
.post("/add-user");
 
// Get the response body as a string
String responseBody = response.getBody().asString();
 
// Print the response body
System.out.println("Response Body: " + responseBody);
 
// Get the status code
int statusCode = response.getStatusCode();
 
// Print the status code
System.out.println("Status Code: " + statusCode);
 
 }

Output