API Automation Testing using Rest Assured
Published: 2023-11-24 10:27:51

API (Application Programming Interface) testing is a crucial aspect of software development, ensuring that the communication between different software components functions as expected. 
Rest Assured is a popular Java-based library for automating API testing, providing a simple and expressive way to validate the behavior of RESTful APIs. 

In this article, we'll guide you through the basics of API automation testing using Rest Assured.

**Prerequisites:**

Before diving into API automation testing with Rest Assured, make sure you have the following prerequisites in place:
1.	**Java Installed:** Rest Assured is a Java library, so you'll need Java installed on your machine.

2.	**Integrated Development Environment (IDE):** Choose an IDE like Eclipse or IntelliJ to write and execute your Java code.

3.	**Rest Assured Library:** Include the Rest Assured library in your project. You can do this by adding the following dependency to your project's build file (Maven or Gradle):

<!-- For Maven -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>4.4.0</version>
    <scope>test</scope>
</dependency>
------------------------------------------------
// For Gradle
testImplementation 'io.rest-assured:rest-assured:4.4.0'

4. **TestNG:** TestNG is a testing framework for Java that simplifies the testing process and makes it more flexible. Add the TestNG library to your project:
 
<!-- For Maven -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
------------------------------------------
// For Gradle
testImplementation 'org.testng:testng:7.4.0'


**Getting Started:**

Now that you have the prerequisites in place, let's create a simple API test using Rest Assured. For this example, we'll use the ReqRes API (https://reqres.in/), a free and public API for testing.
1.	**Write your first API test:**
Create a new Java class in your IDE and write a basic test using Rest Assured:

import io.restassured.RestAssured;
import org.testng.annotations.Test;

public class ApiTest {

    @Test
    public void getUsers() {
        // Specify the base URI of the API
        RestAssured.baseURI = "https://reqres.in/api";

        // Make a GET request to retrieve a list of users (example endpoint)
        RestAssured.given()
                   .when()
                   .get("/users")
                   .then()
                   .statusCode(200); // Ensure that the response code is 200 (OK)
    }
}


2.	**Run the test:**
Execute the test in your IDE. Rest Assured will send a GET request to the specified endpoint, and the test will pass if the response code is 200.

3.	**Assertions and Validations:**
Rest Assured provides expressive methods for asserting and validating API responses. For example, you can check the response body, headers, and more. Here's an extended version of the previous test:

import io.restassured.RestAssured;
import org.testng.annotations.Test;

import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

public class ApiTest {

    @Test
    public void getUsers() {
        RestAssured.baseURI = "https://reqres.in/api";

        RestAssured.given()
                   .when()
                   .get("/users")
                   .then()
                   .statusCode(200)
                   .body("data.id[0]", equalTo(1))
                   .body("data.first_name", hasItems("George", "Janet"))
                   .header("Content-Type", containsString("application/json"));
    }
}

**Conclusion:**
In this article, we've covered the basics of API automation testing using Rest Assured. As you continue your journey with API testing, explore Rest Assured's rich set of features for handling authentication, request and response customization, and more. With Rest Assured, you can build robust and maintainable API test suites to ensure the reliability of your applications. 
Happy testing!
[Testrig Technologies](https://automationtestingcompany.com/)