Setting Up a Rest Assured Project with Maven - Notes By ShariqSP
Setting Up a Rest Assured Project with Maven
Rest Assured is a popular Java library for testing REST APIs. It simplifies the process of sending HTTP requests and verifying responses. This guide will show you how to set up a Maven project for Rest Assured and include a sample test using the API endpoint http://apitesting.shariqsp.com:8080/api/users/all.
Step 1: Create a New Maven Project
- Open your IDE (e.g., IntelliJ, Eclipse).
- Go to File > New > Project and select Maven.
- Fill in the project details:
GroupId: com.exampleArtifactId: rest-assured-demo
- Click Finish to create the project.
Step 2: Add Dependencies to pom.xml
Open the pom.xml file and add the following dependencies for Rest Assured and TestNG:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>rest-assured-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</build>
</project>
Step 3: Create a Sample Test Case
Let's create a test to verify the response from the endpoint /api/users/all. In your Maven project, create a new Java class named UsersApiTest.java under the directory src/test/java.
// Import Rest Assured classes
import org.testng.annotations.Test;
public class UsersApiTest {
@Test
public void testGetAllUsers() {
Response resp= RestAssured.get("http://apitesting.shariqsp.com:8080/api/users/all");
System.out.println(resp.prettyPrint());
}
}
Step 4: Run the Test
To run your test, use the following Maven command:
mvn test
This command will compile your code and execute the test case using TestNG.
Step 5: Analyze the Test Results
Once the test runs, you should see the results in your console. Rest Assured will display the request and response details, which can help you verify if your API is working as expected.
Benefits of Using Rest Assured with Maven
- Automates the process of testing REST APIs with clean and readable syntax.
- Easy dependency management with Maven for quick setup and version control.
- Supports integration with CI/CD pipelines, enabling continuous testing.
- Combines well with testing frameworks like TestNG and JUnit.