Rest Assured Setup in Java using Maven

Setup REST Assured in Java Using Maven

In this tutorial, you’ll setup REST Assured in Java using Maven, add the required dependencies, verify the installation, and execute your first API test.


Prerequisites

Before setting up REST Assured, make sure you have the following installed:

  • Java JDK 11 or later (JDK 17 or 21 is recommended)
  • Apache Maven
  • IntelliJ IDEA or Eclipse
  • Basic knowledge of Java
  • Internet connection to download Maven dependencies

What is Maven?

Apache Maven is a build automation and dependency management tool for Java projects.

Instead of downloading JAR files manually, Maven automatically downloads the required libraries from the Maven Central Repository and manages project dependencies.

Some benefits of using Maven include:

  • Automatic dependency management
  • Easy project builds
  • Version control for libraries
  • Plugin support
  • Standard project structure

For REST Assured projects, Maven simplifies library management and keeps dependencies up to date.


Step 1: Create a Maven Project

Using IntelliJ IDEA

  1. Open IntelliJ IDEA.
  2. Select New Project.
  3. Choose Maven.
  4. Select your installed JDK.
  5. Click Next.
  6. Enter:
    • Group ID: com.example
    • Artifact ID: RestAssuredDemo
  7. Click Finish.

Your project structure will look like this:

RestAssuredDemo
├── src
│ ├── main
│ └── test
├── pom.xml
└── target

The pom.xml file is where Maven dependencies and plugins are configured.


Step 2: Add REST Assured Dependency

Open your pom.xml file and add the REST Assured dependency.

<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.5.6</version>
<scope>test</scope>
</dependency>
</dependencies>

Note: Replace the version number with the latest stable release if a newer version is available.

Save the file and allow Maven to download the required libraries.


Step 3: Add TestNG (Optional)

If you plan to execute tests using TestNG, include the following dependency:

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>

Step 4: Add Hamcrest Matchers

REST Assured uses Hamcrest matchers for assertions.

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>

Example:

.then()
.statusCode(200)
.body("status", equalTo("success"));

Step 5: Refresh Maven Dependencies

If dependencies are not downloaded automatically:

IntelliJ IDEA

  • Right-click the project.
  • Select MavenReload Project.

or

Click the Reload All Maven Projects icon in the Maven tool window.


Eclipse

  • Right-click the project.
  • Select Maven
  • Click Update Project.

Step 6: Verify the Installation

Create a new Java class inside:

src/test/java

Create the file:

FirstAPITest.java

Add the following code:

import io.restassured.RestAssured;
import org.testng.annotations.Test;
public class FirstAPITest {
@Test
public void verifySetup() {
RestAssured
.get("https://reqres.in/api/users?page=2")
.then()
.statusCode(200);
}
}

If the test executes successfully, your REST Assured setup is complete.


Step 7: Understanding the Project Structure

A well-organized project makes API automation easier to maintain.

RestAssuredDemo
├── src
│ ├── main
│ └── test
│ ├── java
│ │ └── tests
│ └── resources
├── pom.xml
└── target

As your project grows, you can introduce additional folders:

tests
base
utils
payloads
config
pojos
reports

This modular structure improves maintainability and scalability.


Common Maven Dependencies for REST Assured Projects

A typical REST Assured framework may include:

DependencyPurpose
REST AssuredAPI automation
TestNGTest execution
HamcrestAssertions
Jackson DatabindJSON serialization and deserialization
JSON Schema ValidatorResponse schema validation
Extent ReportsReporting
AllureAdvanced reporting
Log4j2Logging

Troubleshooting Common Setup Issues

Dependency Not Downloaded

Cause: Maven has not refreshed the project.

Solution:

  • Reload the Maven project.
  • Check your internet connection.
  • Verify that the dependency version is correct.

RestAssured Cannot Be Resolved

Cause:

The dependency is missing or incorrectly configured.

Solution:

  • Confirm the dependency exists in pom.xml.
  • Refresh the project.
  • Rebuild the project.

Java Version Errors

Ensure the Maven compiler plugin targets your installed JDK version.

Example:

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

Replace 17 with the version you are using if different.


Best Practices

  • Use the latest stable version of REST Assured.
  • Keep dependency versions consistent.
  • Store API endpoints in configuration files instead of hardcoding them.
  • Separate test code from reusable utilities.
  • Use Maven to manage all project dependencies.
  • Organize tests into logical packages from the beginning.

Key Takeaways

In this tutorial, you learned how to:

  • Create a Maven project for API automation.
  • Add the REST Assured dependency.
  • Include TestNG and Hamcrest.
  • Refresh Maven dependencies.
  • Verify the installation by running a simple API test.
  • Understand the recommended project structure.
  • Troubleshoot common setup issues.

With your environment configured, you’re ready to start writing API tests using REST Assured.


Frequently Asked Questions

Why should I use Maven with REST Assured?

Maven simplifies dependency management, automates builds, and ensures all required libraries are downloaded and maintained consistently.

Is Maven mandatory for REST Assured?

No. REST Assured can be used without Maven by manually adding JAR files, but Maven is the recommended approach for most Java projects.

Which Java version is recommended?

REST Assured works with modern Java versions. JDK 17 or JDK 21 is recommended for new projects.

Which testing framework should I use?

REST Assured integrates well with TestNG and JUnit. TestNG is a common choice for enterprise API automation due to its support for grouping, parallel execution, and data-driven testing.

What is the next step after setup?

Once your project is configured, the next step is learning how to send and validate your first API request using REST Assured.

Related Articles


Discover more from Rotebit

Subscribe to get the latest posts sent to your email.

1 Comment

Leave a Reply