Maven Dependencies for REST Assured

REST Assured is a Java library, so it is commonly added to a Maven project using the pom.xml file.

To build a complete API automation framework, REST Assured is typically used along with additional dependencies for JSON/XML processing, testing, reporting, and logging.


Interview Answer

"To use REST Assured in a Maven project, I add the REST Assured dependency in the pom.xml. Depending on the project requirements, I also include json-path, xml-path, Groovy, and TestNG or JUnit dependencies. Maven automatically downloads these libraries, making the project easy to set up and maintain."

Advertisement

Core REST Assured Dependency

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

JSON Path Dependency

Used for reading and validating JSON responses.

 
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-path</artifactId>
    <version>5.5.0</version>
    <scope>test</scope>
</dependency>
 

XML Path Dependency

Used for validating XML responses.

 
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>xml-path</artifactId>
    <version>5.5.0</version>
    <scope>test</scope>
</dependency>
 

JSON Schema Validator Dependency

Used to validate API responses against JSON Schema.

 
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>5.5.0</version>
    <scope>test</scope>
</dependency>
 

TestNG Dependency

If your framework uses TestNG:

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

JUnit Dependency

If your framework uses JUnit:

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

Groovy Dependency

REST Assured internally uses Groovy, so some projects include it explicitly if required.

 
<dependency>
    <groupId>org.apache.groovy</groupId>
    <artifactId>groovy</artifactId>
    <version>5.0.1</version>
</dependency>
 

Common Additional Dependencies

Most REST Assured frameworks also include:

  • Maven Surefire Plugin
  • Log4j
  • Jackson Databind
  • Extent Reports
  • Allure Reports
  • Apache POI (Excel)
  • Lombok (Optional)

Maven Dependency Flow

 
pom.xml
    │
    ▼
Maven Downloads Dependencies
    │
    ▼
REST Assured
JSON Path
XML Path
TestNG / JUnit
Groovy
    │
    ▼
Ready for API Automation
 

Why Use Maven?

Maven provides:

  • Automatic dependency management
  • Version management
  • Easy project setup
  • Build automation
  • Test execution
  • CI/CD integration

Prerequisites for REST Assured

REST Assured requires only a basic Java development environment.


Software Requirements

You should have:

  • Java Development Kit (JDK)
  • Java IDE (IntelliJ IDEA or Eclipse)
  • Maven
  • REST Assured dependencies
  • TestNG or JUnit

Knowledge Prerequisites

Basic understanding of:

  • Java
  • REST APIs
  • HTTP Methods
  • JSON
  • Maven

REST Assured Setup

 
JDK
   │
   ▼
Java IDE
   │
   ▼
Maven Project
   │
   ▼
Add REST Assured Dependencies
   │
   ▼
Write API Test Cases
 

Does REST Assured Require a Server?

No.

REST Assured is a lightweight Java library.

It does not require:

  • Web Server
  • Application Server
  • API Server Installation

You only need:


Advantages

  • Easy setup
  • Lightweight
  • Open source
  • Java-based
  • No server installation required

The given(), when(), then() Syntax

REST Assured follows the BDD (Behavior Driven Development) approach.

It organizes every API test into three logical sections.

 
given()
      │
      ▼
when()
      │
      ▼
then()
 

Interview Answer

"given(), when(), and then() are the three core methods in REST Assured's BDD syntax. given() defines the request details such as headers, parameters, authentication, and request body. when() sends the HTTP request. then() validates the response, including the status code, response body, and headers."


given()

The given() section prepares the request.

It specifies:

  • Base URI
  • Headers
  • Authentication
  • Query Parameters
  • Path Parameters
  • Cookies
  • Payload

Example

 
given()
    .header("Authorization", "Bearer " + token)
    .contentType(ContentType.JSON)
    .body(payload);
 

when()

The when() section sends the actual HTTP request.

Supported methods include:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

Example

 
.when()
.post("/users");
 

then()

The then() section validates the response.

Common validations include:

  • Status Code
  • Response Body
  • Headers
  • Cookies
  • Response Time

Example

 
.then()
.statusCode(201);
 

Complete Example

 
given()
        .contentType(ContentType.JSON)
        .body(payload)

.when()
        .post("/users")

.then()
        .statusCode(201)
        .body("name", equalTo("John"));
 

Pizza Ordering Analogy

Think of ordering a pizza.

 
given()
 

You provide:

  • Delivery address
  • Pizza type
  • Payment method

 
when()
 

You place the order.

 
then()
 

You verify:

  • Correct pizza
  • Correct size
  • Delivered successfully

The same idea applies to REST Assured.


BDD Flow

 
given()
│
├── Headers
├── Authentication
├── Parameters
├── Payload
│
▼
when()
│
├── GET
├── POST
├── PUT
├── DELETE
│
▼
then()
│
├── Status Code
├── Response Body
├── Headers
├── Cookies
└── Response Time
 

Advantages of BDD Syntax

  • Easy to read
  • Easy to understand
  • Clean structure
  • Better maintenance
  • Natural English-like syntax

Configuring REST Assured with TestNG

REST Assured integrates seamlessly with TestNG for test execution and reporting.


Step 1: Create a Maven Project

Create a Maven project using:

  • IntelliJ IDEA
  • Eclipse
  • VS Code (with Maven support)

Step 2: Add Dependencies

Include:

inside the pom.xml.


Step 3: Create Test Class

 
public class UserTest {

    @Test
    public void verifyUser(){

    }

}
 

Step 4: Write REST Assured Test

 
@Test
public void getUser(){

    given()

    .when()
            .get("/users/2")

    .then()
            .statusCode(200);

}
 

Step 5: Create testng.xml

 
<suite name="REST Suite">

    <test name="API Tests">

        <classes>

            <class name="tests.UserTest"/>

        </classes>

    </test>

</suite>
 

Step 6: Execute Tests

Execution options:

  • IDE
  • Maven
 
mvn test
 

TestNG Execution Flow

 
Maven Project
      │
      ▼
REST Assured Test
      │
      ▼
@Test Method
      │
      ▼
testng.xml
      │
      ▼
Execute Tests
      │
      ▼
Generate Reports
 

Advantages of TestNG

  • Test grouping
  • Parallel execution
  • Data Providers
  • Retry Analyzer
  • Test reports
  • Flexible suite execution

Interview Answer

"I configure REST Assured with TestNG by creating a Maven project, adding the REST Assured and TestNG dependencies in the pom.xml, writing API test cases inside methods annotated with @Test, and managing execution through the testng.xml suite file."


Configuring REST Assured with JUnit

The setup is almost identical to TestNG.

The main difference is that JUnit uses its own annotations and assertions.


Step 1: Add Dependencies

Include:

inside the pom.xml.


Step 2: Create Test Class

 
public class UserTest {

    @Test
    void verifyUser(){

    }

}
 

Step 3: Write REST Assured Test

 
@Test
void getUser(){

    given()

    .when()
            .get("/users/2")

    .then()
            .statusCode(200);

}
 

Step 4: Execute Tests

Run using:

  • IDE
  • Maven
 
mvn test
 
  • Jenkins

TestNG vs JUnit

Feature TestNG JUnit
Annotation @Test @Test
Suite File testng.xml Not Required
Data Provider Yes Parameterized Tests
Retry Support Yes Requires Extension
Parallel Execution Built-in Supported (JUnit 5)
Popular for Automation Yes Yes

Best Practices

  • Use Maven for dependency management.
  • Keep all dependencies in the pom.xml.
  • Use the latest stable library versions.
  • Prefer TestNG for large automation frameworks.
  • Use the given(), when(), then() structure consistently.
  • Organize dependencies and plugins clearly in the pom.xml.

Frequently Asked Questions (FAQs)

1. What is the Maven dependency for REST Assured?

Add the REST Assured dependency in the pom.xml. Depending on the project, include additional dependencies such as json-path, xml-path, json-schema-validator, Groovy, and either TestNG or JUnit for test execution.


2. What are the prerequisites for REST Assured?

REST Assured requires:

  • Java Development Kit (JDK)
  • Java IDE (IntelliJ IDEA or Eclipse)
  • Maven project
  • REST Assured dependencies
  • TestNG or JUnit
  • Basic knowledge of REST APIs and JSON

No server installation is required.


3. What is the purpose of given(), when(), and then()?

REST Assured follows the BDD style:

  • given() → Configures the request (headers, parameters, authentication, payload).
  • when() → Sends the HTTP request.
  • then() → Validates the response (status code, body, headers, response time, etc.).

4. How do you configure REST Assured with TestNG?

Create a Maven project, add the REST Assured and TestNG dependencies in the pom.xml, write API tests using REST Assured inside methods annotated with @Test, and manage execution using the testng.xml suite file.


5. How do you configure REST Assured with JUnit?

Create a Maven project, add the REST Assured and JUnit dependencies, write API test methods using JUnit's @Test annotation, and validate responses using REST Assured assertions. Tests can be executed from the IDE, Maven, or Jenkins.


6. Does REST Assured require a server setup?

No. REST Assured is a lightweight Java library and does not require a dedicated server. You only need a JDK, a Java IDE, a Maven project with the required dependencies, and access to the API endpoints you want to test.