Introduction
In our previous post, we covered the Installation and Setup of REST Assured, including creating a Maven project and running a basic test. Now, we’ll focus on Maven Dependencies, which are crucial for integrating REST Assured and related libraries into your project. This guide is designed for beginners and experienced developers, explaining how to manage dependencies in your pom.xml file with clear examples and best practices.
Key Point: Maven dependencies allow you to add libraries like REST Assured and testing frameworks to your project without manually downloading JAR files, ensuring compatibility and easy updates.
What Are Maven Dependencies?
Maven uses a pom.xml (Project Object Model) file to manage project configurations, including dependencies. A dependency is a library or module your project needs, such as REST Assured for API testing or JUnit for test execution. Maven downloads these libraries from a central repository (e.g., Maven Central) and manages their versions, transitive dependencies, and conflicts automatically.
For REST Assured, you’ll need its core dependency and, optionally, dependencies for testing frameworks, JSON/XML parsing, or additional features like Hamcrest matchers.
Step 1: Add the Core REST Assured Dependency
To use REST Assured, you must include its dependency in the pom.xml
file. Open your project’s pom.xml
and add the following within the <dependencies>
section.
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
Explanation:
groupId
: Identifies the 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 the dependency to the test phase, as REST Assured is primarily used for testing.
Important: Always check the Maven Central Repository for the latest version of REST Assured to ensure you’re using the most up-to-date and secure version.
Step 2: Add a Testing Framework Dependency
REST Assured works seamlessly with testing frameworks like JUnit or TestNG to structure and run your tests. Below are the dependencies for each.
JUnit 5
For JUnit 5 (Jupiter), 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 includes the JUnit 5 engine and API for writing and running tests.
TestNG (Alternative)
If you prefer TestNG, add this dependency instead:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
Note: Choose either JUnit or TestNG based on your project needs. JUnit is more common for beginners, while TestNG offers advanced features like parallel execution.
Step 3: Add Hamcrest for Assertions
REST Assured often uses Hamcrest matchers for flexible response validations (e.g., equalTo
, notNullValue
). The core REST Assured dependency includes Hamcrest, but you can explicitly add it for clarity or to use a specific version.
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
Hamcrest enhances your tests by providing readable assertions, as shown in the example below.
Step 4: Example Test with Dependencies
Let’s verify the dependencies with a sample test using REST Assured, JUnit 5, and Hamcrest. Create a Java class in src/test/java
(e.g., com.example.DependencyTest
) 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 DependencyTest {
@Test
public void testUserEndpoint() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
given()
.when()
.get("/users/1")
.then()
.statusCode(200)
.body("id", equalTo(1))
.body("name", notNullValue())
.body("address.city", equalTo("Gwenborough"));
}
}
Explanation:
RestAssured.baseURI
: Sets the base URL for the public API.given().when().get()
: Sends a GET request to the/users/1
endpoint.then()
: Validates the response using Hamcrest matchers:statusCode(200)
: Ensures a successful response.body("id", equalTo(1))
: Checks if the user ID is 1.body("name", notNullValue())
: Verifies the name field exists.body("address.city", equalTo("Gwenborough"))
: Validates a nested field in the JSON response.
Run the test using your IDE or mvn test
. If the dependencies are correctly configured, the test will pass.
Troubleshooting Tip: If you encounter errors, ensure Maven has downloaded all dependencies by runningmvn clean install
. Check for version conflicts inpom.xml
or verify your internet connection.
Step 5: Managing Dependency Versions
To avoid version conflicts, consider using a dependency management section in pom.xml
. This centralizes version numbers for all dependencies. Here’s an example:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</dependencyManagement>
This ensures all dependencies use consistent versions, reducing conflicts when adding more libraries later.
Tips for Beginners
- Check Versions: Regularly update dependency versions via Maven Central.
- Minimize Dependencies: Only include necessary libraries to keep your project lightweight.
- Use IDE Tools: IDEs like IntelliJ IDEA suggest dependencies and highlight errors in
pom.xml
. - Test Early: Run a simple test after adding dependencies to confirm they work.
What’s Next?
In the next post, we’ll explore Configuration (SSL, Proxy), covering how to configure REST Assured for secure connections and proxy settings. Stay tuned for more hands-on examples!