TecNimbus

Let’s build better software—and a more balanced life—together.

, ,

How to Run API Tests Using Rest Assured (with Spring Boot and Maven )

One of the most popular tools for automated API testing in Java is Rest Assured — a powerful and easy-to-use library that allows you to write API tests directly in code. It supports HTTP methods, request validation, and detailed assertions in a clean, readable syntax.

In this article, we’ll walk through how to set up and run API tests using Rest Assured with simple examples based on the PetStore API, a well-known demo application for practicing CRUD operations.

⚙️ Prerequisites

Run the Petstore API Demo Locally

First, let’s start by running a Petstore API Demo that we can test.
If you haven’t already, clone and run the Petstore API Demo

Once the application is running, you can open:

make sure you have:

  • Java 17+ installed
  • Maven or (Gradle) for project setup
  • A code editor like IntelliJ IDEA or VS Code with Java extensions
  • Basic understanding of REST APIs

✅ 1. Set Up Your Maven Project

<dependencies>
  <dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.4.0</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.10.2</version>
    <scope>test</scope>
  </dependency>
</dependencies>

This will include Rest Assured for API testing and TestNG for test organization and execution.

✅ 2. Write Your API Tests

Let’s write a few API test scenarios for the PetStore API, similar to what we did in Postman.

We’ll cover:

  • Add a new pet to the store
  • Update an existing pet
  • Find pet by ID
  • Delete Pet
  • Pet not found
package com.petstore.rest_assured.api_tests;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class PetStoreApiTests {

    private static Long petId;

    @BeforeClass
    public void setup() {
        RestAssured.baseURI = "http://localhost:8080/api/v1";
    }

    // Add a new pet
    @Test(priority = 1)
    public void testAddNewPet() {
        String requestBody = """
        {
          "id": 4677,
          "name": "fluffy",
          "status": "New",
          "photoUrls": [
            "Photo1",
            "Photo1"
          ],
          "category": {
            "id": 9342,
            "name": "ABC"
          },
          "tags": [
            {
              "name": "PET",
              "id": 2875
            },
            {
              "name": "STORE",
              "id": 7063
            }
          ]
        }
        """;
        Response response = given()
                .contentType("application/json")
                .body(requestBody)
                .when()
                .post("/pet")
                .then()
                .statusCode(200)
                .body("name", equalTo("fluffy"))
                .extract().response();

        petId = response.jsonPath().getLong("id");
        System.out.println("Created Pet ID: " + petId);
    }

    // Update an existing pet
    @Test(priority = 2, dependsOnMethods = "testAddNewPet")
    public void testUpdatePet() {
        String requestBody = String.format("""
        {
          "id": %d,
          "name": "fluffy-updated",
          "status": "SOLD",
          "photoUrls": [
            "Photo1",
            "Photo1"
          ],
          "category": {
            "id": 9342,
            "name": "ABC"
          },
          "tags": [
            {
              "name": "PET",
              "id": 2875
            },
            {
              "name": "STORE",
              "id": 7063
            }
          ]
        }
        """, petId);

        given()
                .contentType("application/json")
                .body(requestBody)
                .when()
                .put("/pet")
                .then()
                .statusCode(200)
                .body("name", equalTo("fluffy-updated"))
                .body("status", equalTo("SOLD"));
    }

    // Find pet by ID
    @Test(priority = 3, dependsOnMethods = "testUpdatePet")
    public void testFindPetById() {
        given()
                .pathParam("petId", petId)
                .when()
                .get("/pet/{petId}")
                .then()
                .statusCode(200)
                .body("id", equalTo(petId.intValue()));
    }

    // Delete Pet
    @Test(priority = 4, dependsOnMethods = "testFindPetById")
    public void testDeletePet() {
        given()
                .pathParam("petId", petId)
                .when()
                .delete("/pet/{petId}")
                .then()
                .statusCode(200);
    }

    // Pet not found
    @Test(priority = 5)
    public void testPetNotFound() {
        given()
                .pathParam("petId", petId)
                .when()
                .get("/pet/{petId}")
                .then()
                .statusCode(404)
                .body("message", equalTo("Pet not found with id: " + petId));
    }
}

✅ 3. Run the Test

To execute your tests, open a terminal in your project directory and run: below command or run it off form your IDE

mvn test

🧭 Conclusion

Rest Assured is a powerful tool for automating API testing directly within your codebase.
Unlike Postman collections, it allows you to:

  • Write tests programmatically
  • Reuse code and data structures
  • Integrate easily into CI/CD pipelines

If you’re comfortable with Java, this is one of the most scalable ways to build automated API test suites.

Resources

API-Testing-With-Rest-Assured