If you're looking to automate API testing, REST Assured is one of the most popular Java libraries for testing RESTful APIs. In this guide, we'll walk through setting up a basic REST Assured project in Eclipse IDE using Maven, which makes it easier to manage dependencies and build configurations.
Prerequisites
Before starting, ensure you have the following installed on your system:
- Java Development Kit (JDK) 8 or higher
- Eclipse IDE for Java Developers
- Maven (If not bundled with Eclipse, you can install it separately.)
Step 1: Create a Maven Project in Eclipse
- Open Eclipse IDE.
- Go to File > New > Other…
- In the New Project Wizard, search for Maven and select Maven Project. Click Next.
- In the next window, you can select the workspace location and click Next again.
- In the Artifact Id field, enter a name for your project, for example,
rest-assured-basic
. - Set Group Id (e.g.,
com.example.api
). You can also provide a version if needed. - Click Finish to create the Maven project.
Step 2: Update pom.xml
with REST Assured Dependencies
To use REST Assured, you need to add it as a dependency in your Maven project. This dependency will allow Maven to download and manage the necessary libraries.
1. Open the pom.xml
file in your project.
2. Inside the <dependencies>
tag, add the following REST Assured dependency:
pom.xml
file. Maven will automatically download and include the REST Assured library in your project.Step 3: Verify the Dependency Setup
After saving pom.xml
, ensure the REST Assured library is successfully added:
- Expand Maven Dependencies under your project in the Project Explorer.
- Check for REST Assured in the dependencies list. If it’s not there, right-click the project, select Maven > Update Project, and refresh.
Step 4: Write a Simple REST Assured Test
Now that the setup is complete, let's create a basic test case using REST Assured.
In src/test/java, create a new package (e.g.,
com.example.api.tests
).Inside this package, create a new Java class, for example,
BasicApiTest
.In this class, write a simple GET request test. Here’s an example that sends a request to a public API and validates the status code:
Step 5: Run the Test
To execute your REST Assured test:
- Right-click on
BasicApiTest
and select Run As > TestNG Test (you can use JUnit if your setup requires it). - Check the Console output to see the response and any assertions.
If the setup is correct, your test should pass, and you should see the JSON response in the console.
Step 6: Advanced Configurations (Optional)
Once you've verified the basic setup, here are some additional configurations and tips:
- Logging: REST Assured provides logging methods like
.log().all()
that can help in debugging requests and responses. - Parameterized Tests: You can use frameworks like TestNG for parameterized tests to cover multiple API endpoints in a single test class.
- Base URI and Paths: Instead of setting
baseURI
in each test, consider using a base setup method or properties file.