Skip to main content

๐Ÿ› ๏ธ Project Setup with Maven and Gradle

๐Ÿ“˜ Introductionโ€‹

Before you can start writing API tests with RestAssured, you need to set up your project environment. This involves configuring a build tool like Maven or Gradle, adding the necessary dependencies, and organizing your project structure. Both Maven and Gradle are widely used in Java projects, and they simplify dependency management, compilation, and execution of tests.

In this section, weโ€™ll walk through the steps to set up a project using both Maven and Gradle, including adding RestAssured dependencies and configuring your IDE.


๐Ÿš€ Why Use Maven or Gradle?โ€‹

Build tools like Maven and Gradle streamline the development process by:

  1. Managing Dependencies
    Automatically download and include libraries (e.g., RestAssured, TestNG) required for your project

  2. Standardizing Project Structure
    Provide a consistent directory layout for source code, tests, and resources

  3. Simplifying Builds
    Compile, test, and package your project with a single command

  4. Integration with CI/CD
    Easily integrate with tools like Jenkins, GitLab CI, and others for automated builds and testing


โš™๏ธ Setting Up with Mavenโ€‹

Step 1: Install Mavenโ€‹

  • Download and install Maven from the official website
  • Verify the installation:
mvn -v

Step 2: Create a Maven Projectโ€‹

mvn archetype:generate -DgroupId=com.example -DartifactId=api-tests -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Project Structure

api-tests/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ main/
โ”‚ โ””โ”€โ”€ test/
โ”œโ”€โ”€ pom.xml

Step 3: Add RestAssured Dependenciesโ€‹

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

<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.0</version>
<scope>test</scope>
</dependency>

<!-- JSON Schema Validation -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>5.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

Step 4: Configure Your IDEโ€‹

  • IntelliJ IDEA โ†’ File > Open > Select project folder
  • Eclipse โ†’ File > Import > Existing Maven Project

โš™๏ธ Setting Up with Gradleโ€‹

Step 1: Install Gradleโ€‹

gradle -v

Step 2: Create a Gradle Projectโ€‹

gradle init --type java-application

Project Structure

api-tests/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ main/
โ”‚ โ””โ”€โ”€ test/
โ”œโ”€โ”€ build.gradle

Step 3: Add RestAssured Dependenciesโ€‹

plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
// RestAssured
testImplementation 'io.rest-assured:rest-assured:5.3.0'

// TestNG
testImplementation 'org.testng:testng:7.7.0'

// JSON Schema Validation
testImplementation 'io.rest-assured:json-schema-validator:5.3.0'
}

Step 4: Configure Your IDEโ€‹

  • IntelliJ IDEA โ†’ File > Open > Select project folder
  • Eclipse โ†’ Install Buildship plugin and import as Gradle project

๐Ÿงช Verifying the Setupโ€‹

1. Create a Simple Testโ€‹

import io.restassured.RestAssured;
import org.testng.annotations.Test;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class FirstAPITest {

@Test
public void testGetUsers() {
RestAssured.baseURI = "https://reqres.in/api";

given()
.when()
.get("/users")
.then()
.statusCode(200)
.body("data[0].first_name", equalTo("George"));
}
}

2. Run the Testโ€‹

Maven

mvn test

Gradle

gradle test

3. Check the Resultsโ€‹

  • If the setup is correct, the test should pass successfully

โœ… Best Practices for Project Setupโ€‹

  1. Use Version Control
git init
git add .
git commit -m "Initial project setup"
  1. Organize Tests
    Place tests under src/test/java with meaningful packages

  2. Externalize Configuration
    Use config files or environment variables

  3. Automate Builds
    Integrate with CI/CD tools

  4. Keep Dependencies Updated


๐Ÿ Conclusionโ€‹

Setting up your project with Maven or Gradle is the first step toward building a robust API automation framework. By following the steps outlined above, you can configure your environment, add RestAssured dependencies, and write your first API test. In the next section, weโ€™ll dive deeper into the given-when-then structure of RestAssured and explore how to write more complex API tests.