Setting Up a Basic REST Assured Maven Project in Eclipse IDE

If you're looking to automate API testing, REST Assured is one of the most popular Java libraries for testing RESTful APIs. In this guide, we'll walk through setting up a basic REST Assured project in Eclipse IDE using Maven, which makes it easier to manage dependencies and build configurations.

Prerequisites

Before starting, ensure you have the following installed on your system:

  1. Java Development Kit (JDK) 8 or higher
  2. Eclipse IDE for Java Developers
  3. Maven (If not bundled with Eclipse, you can install it separately.)

Step 1: Create a Maven Project in Eclipse

  1. Open Eclipse IDE.
  2. Go to File > New > Other…
  3. In the New Project Wizard, search for Maven and select Maven Project. Click Next.
  4. In the next window, you can select the workspace location and click Next again.
  5. In the Artifact Id field, enter a name for your project, for example, rest-assured-basic.
  6. Set Group Id (e.g., com.example.api). You can also provide a version if needed.
  7. Click Finish to create the Maven project.

Step 2: Update pom.xml with REST Assured Dependencies

To use REST Assured, you need to add it as a dependency in your Maven project. This dependency will allow Maven to download and manage the necessary libraries.

1. Open the pom.xml file in your project.

2. Inside the <dependencies> tag, add the following REST Assured dependency:

<dependencies>
    <!-- REST Assured Dependency -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.3.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

3. Save the pom.xml file. Maven will automatically download and include the REST Assured library in your project.

Step 3: Verify the Dependency Setup

After saving pom.xml, ensure the REST Assured library is successfully added:

  1. Expand Maven Dependencies under your project in the Project Explorer.
  2. Check for REST Assured in the dependencies list. If it’s not there, right-click the project, select Maven > Update Project, and refresh.

Step 4: Write a Simple REST Assured Test

Now that the setup is complete, let's create a basic test case using REST Assured.

  1. In src/test/java, create a new package (e.g., com.example.api.tests).

  2. Inside this package, create a new Java class, for example, BasicApiTest.

  3. In this class, write a simple GET request test. Here’s an example that sends a request to a public API and validates the status code:

package com.example.api.tests;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.Test;

public class BasicApiTest {

    @Test
    public void testGetRequest() {
        // Define the base URI of the API
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";

        // Send a GET request to /posts endpoint
        Response response = RestAssured.given()
                .when()
                .get("/posts/1");

        // Print the response body for debugging
        System.out.println("Response Body:");
        System.out.println(response.getBody().asPrettyString());

        // Assert the status code
        int statusCode = response.getStatusCode();
        Assert.assertEquals(statusCode, 200, "Status code should be 200");
    }
}

Step 5: Run the Test

To execute your REST Assured test:

  1. Right-click on BasicApiTest and select Run As > TestNG Test (you can use JUnit if your setup requires it).
  2. Check the Console output to see the response and any assertions.

If the setup is correct, your test should pass, and you should see the JSON response in the console.

Step 6: Advanced Configurations (Optional)

Once you've verified the basic setup, here are some additional configurations and tips:

  1. Logging: REST Assured provides logging methods like .log().all() that can help in debugging requests and responses.
  2. Parameterized Tests: You can use frameworks like TestNG for parameterized tests to cover multiple API endpoints in a single test class.
  3. Base URI and Paths: Instead of setting baseURI in each test, consider using a base setup method or properties file.

Followers