๐ ๏ธ 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:
-
Managing Dependencies
Automatically download and include libraries (e.g., RestAssured, TestNG) required for your project -
Standardizing Project Structure
Provide a consistent directory layout for source code, tests, and resources -
Simplifying Builds
Compile, test, and package your project with a single command -
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โ
- Use Version Control
git init
git add .
git commit -m "Initial project setup"
-
Organize Tests
Place tests undersrc/test/javawith meaningful packages -
Externalize Configuration
Use config files or environment variables -
Automate Builds
Integrate with CI/CD tools -
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.