Introduction

In our previous post, we explored REST Assured and its role in simplifying API testing. Now, it’s time to set up your environment to start writing REST Assured tests. This guide walks you through installing and configuring REST Assured using Maven, ensuring you’re ready to test APIs. Whether you’re a beginner or an experienced developer, this step-by-step tutorial is designed to be clear and practical, with examples to get you started.

Key Point: Setting up REST Assured is straightforward with Maven, which automates dependency management and simplifies project configuration.

Prerequisites

Before installing REST Assured, ensure you have the following tools installed:

  • Java Development Kit (JDK): Version 8 or higher. REST Assured requires Java to run.
  • Maven: A build tool to manage dependencies and run tests.
  • Integrated Development Environment (IDE): IntelliJ IDEA, Eclipse, or any Java-compatible IDE.
  • Internet Connection: Needed to download Maven dependencies.
Important: Verify your Java installation by running java -version in a terminal or command prompt. If Java is not installed, download it from the official Oracle website or use OpenJDK. Similarly, check Maven with mvn -version.

Step 1: Install Java and Maven

If you don’t have Java or Maven installed, follow these steps:

  1. Install Java:
    • Download the JDK from the Oracle website or OpenJDK.
    • Follow the installation instructions for your operating system (Windows, macOS, or Linux).
    • Set the JAVA_HOME environment variable to point to your JDK installation directory.
  2. Install Maven:
    • Download Maven from the official Apache Maven website.
    • Extract the archive and add the Maven bin directory to your system’s PATH.
    • Verify installation by running mvn -version in a terminal.

Step 2: Create a Maven Project

Maven simplifies dependency management by downloading the REST Assured library and its dependencies automatically. Let’s create a Maven project in your IDE.

  1. Open your IDE (e.g., IntelliJ IDEA or Eclipse).
  2. Select Create New Project and choose Maven as the project type.
  3. Configure the project:
    • GroupId: A unique identifier for your project (e.g., com.example).
    • ArtifactId: The project name (e.g., rest-assured-demo).
    • JDK: Select JDK 8 or higher.
  4. Click Finish to generate the project structure, including a pom.xml file.

The pom.xml file is the heart of a Maven project, where you define dependencies and configurations.

Step 3: Add REST Assured Dependency

To use REST Assured, add its dependency to the pom.xml file. Open pom.xml in your IDE and include the following within the <dependencies> section.

```xml io.rest-assured rest-assured 5.4.0 test ```

Explanation:

  • groupId: Identifies the library’s organization (io.rest-assured).
  • artifactId: Specifies the library name (rest-assured).
  • version: Uses the latest stable version (5.4.0 as of June 2025).
  • scope>test: Limits REST Assured to the test phase, as it’s primarily used for testing.
Pro Tip: Always check the Maven Repository for the latest REST Assured version to ensure compatibility and security updates.

Step 4: Add Testing Framework Dependency (Optional)

REST Assured integrates well with testing frameworks like JUnit or TestNG. For this example, let’s add JUnit 5 to run our tests. Add the following dependency to pom.xml:


<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.10.2</version>
    <scope>test</scope>
</dependency>

This allows you to write and run REST Assured tests using JUnit 5’s annotations and assertions.

Step 5: Verify the Setup with a Sample Test

To confirm your setup, let’s write a simple REST Assured test that sends a GET request to a public API (https://jsonplaceholder.typicode.com) and validates the response.

Create a new Java class in the src/test/java directory (e.g., com.example.FirstTest) and add the following code:


package com.example;

import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class FirstTest {

    @Test
    public void testGetUser() {
        // Set the base URI
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";

        // Send GET request and validate response
        given()
            .when()
                .get("/users/1")
            .then()
                .statusCode(200)
                .body("id", equalTo(1))
                .body("name", notNullValue())
                .body("email", containsString("@"));
    }
}

Explanation:

  • RestAssured.baseURI: Sets the API’s base URL to avoid repetition.
  • given().when().get(): Sends a GET request to /users/1.
  • then(): Validates the response:
    • statusCode(200): Checks for a successful response.
    • body("id", equalTo(1)): Verifies the user ID is 1.
    • body("name", notNullValue()): Ensures the name field exists.
    • body("email", containsString("@")): Confirms the email contains an "@".

Step 6: Run the Test

To run the test:

  1. Ensure your IDE has synchronized the Maven project (click the "Reload Project" button in IntelliJ or "Update Project" in Eclipse).
  2. Right-click the FirstTest.java file and select Run.
  3. Alternatively, run mvn test in the terminal from your project’s root directory.

If the setup is correct, the test will pass, confirming that REST Assured is properly configured.

Troubleshooting Tip: If the test fails, check your internet connection, verify the API URL, and ensure all dependencies are downloaded by running mvn clean install.

Tips for Beginners

  • Keep pom.xml Clean: Only add necessary dependencies to avoid conflicts.
  • Update Dependencies: Regularly check for newer versions of REST Assured and JUnit.
  • Use an IDE: IntelliJ IDEA or Eclipse simplifies project setup and test execution.
  • Practice: Modify the sample test to explore other endpoints or validations.

What’s Next?

In the next post, we’ll dive into Maven Dependencies, exploring how to manage additional libraries for REST Assured and optimize your project setup. Stay tuned for more practical examples!