What is REST Assured?
REST Assured is a Java-based library designed to simplify testing of RESTful APIs. It provides a user-friendly Domain-Specific Language (DSL) that allows developers and testers to write clean, readable, and maintainable API tests. Whether you’re a beginner learning API testing or an experienced professional automating complex test scenarios, REST Assured makes it easy to send HTTP requests and validate responses without dealing with low-level HTTP protocols.
REST Assured supports various HTTP methods like GET, POST, PUT, and DELETE, and it handles data formats such as JSON and XML seamlessly. Its intuitive syntax and powerful features make it a go-to tool for Java developers working on API testing.
Key Point: REST Assured is built for Java, so you need basic Java knowledge to use it effectively. Don’t worry if you’re new to Java—its syntax is straightforward, and we’ll guide you with examples!
Why Choose REST Assured?
API testing can be complex when using raw HTTP clients or manually parsing responses. REST Assured simplifies this process with the following benefits:
- Readable Syntax: Write tests in a fluent, easy-to-understand format.
- Response Validation: Validate status codes, headers, and response bodies effortlessly.
- Data Format Support: Work with JSON, XML, and other formats natively.
- Testing Framework Integration: Seamlessly integrates with TestNG and JUnit.
- Extensibility: Supports advanced features like authentication, logging, and filters.
For beginners, REST Assured reduces the need for boilerplate code, while experienced testers benefit from its flexibility for complex test cases.
How Does REST Assured Work?
REST Assured uses a Given-When-Then structure, inspired by Behavior-Driven Development (BDD), to make tests intuitive:
- Given: Define the request details, such as the URL, headers, or parameters.
- When: Send the HTTP request (e.g., GET or POST).
- Then: Validate the response, such as checking the status code or response data.
Important: The Given-When-Then syntax makes your tests read like a sentence, improving collaboration between developers, testers, and non-technical stakeholders.
Example: Testing a GET Request with REST Assured
Let’s dive into a simple example to see REST Assured in action. We’ll test a public API to fetch user data from https://jsonplaceholder.typicode.com, a free API for practice.
Prerequisites
Before running the example, ensure you have:
- Java: JDK 8 or higher installed.
- Maven: For managing dependencies (we’ll cover setup in the next post).
- IDE: IntelliJ IDEA, Eclipse, or any Java-compatible IDE.
Step 1: Add REST Assured Dependency
To use REST Assured, you need its dependency in your Maven project. Add the following to your pom.xml
file. (Don’t worry about the setup details yet—we’ll cover them in the next blog post.)
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
Step 2: Write a REST Assured Test
Below is a Java test that sends a GET request to fetch user data and validates the response.
import io.restassured.RestAssured;
import org.junit.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class FirstRestAssuredTest {
@Test
public void testGetUser() {
// Set the base URI for the API
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
// Given-When-Then structure
given()
.when()
.get("/users/1")
.then()
.statusCode(200) // Validate status code is 200 OK
.body("id", equalTo(1)) // Check if user ID is 1
.body("name", notNullValue()) // Ensure name is not null
.body("email", containsString("@")); // Verify email contains '@'
}
}
Explanation of the Code
- Imports: Include REST Assured and Hamcrest matchers for assertions.
- Base URI: Set the API’s base URL to avoid repeating it in requests.
- Given-When-Then:
given()
: Prepares the request (no parameters in this case).when().get("/users/1")
: Sends a GET request to the/users/1
endpoint.then()
: Validates the response:statusCode(200)
: Ensures the response status is 200 OK.body("id", equalTo(1))
: Verifies theid
field is 1.body("name", notNullValue())
: Checks that thename
field exists.body("email", containsString("@"))
: Confirms theemail
field contains an "@" symbol.
Step 3: Run the Test
Run the test using your IDE’s test runner or Maven command (mvn test
). If the API is accessible, the test will pass, confirming the response matches the expected criteria.
Pro Tip: Use a public API like jsonplaceholder.typicode.com
for practice. It’s free, reliable, and doesn’t require authentication.
Key Features of REST Assured
Here’s why REST Assured stands out for API testing:
- Fluent API: Chain methods for concise, readable tests.
- JSON/XML Parsing: Easily extract and validate data using JSON Path or XML Path.
- Authentication: Supports Basic, OAuth2, and other authentication types.
- Logging: Log requests and responses for debugging.
- Integration: Works with CI/CD pipelines and testing frameworks.
When to Use REST Assured?
REST Assured is perfect for:
- Testing RESTful APIs in Java-based projects.
- Automating API tests in CI/CD pipelines.
- Validating complex JSON or XML responses.
- Projects using TestNG or JUnit.
If you’re working in a non-Java environment, consider tools like Postman or Python’s requests library, but for Java developers, REST Assured is a top choice.
Tips for Beginners
- Start Simple: Begin with GET requests, as shown in the example.
- Learn the Syntax: Practice the Given-When-Then structure.
- Use Public APIs: Experiment with free APIs like
jsonplaceholder.typicode.com
. - Explore Documentation: Visit the official REST Assured documentation for detailed guides.
- Experiment: Try validating headers or nested JSON fields to build confidence.
What’s Next?
In the next post, we’ll cover Installation and Setup for REST Assured, including how to set up a Maven project, add dependencies, and configure your environment. Stay tuned for a detailed guide to kickstart your API testing journey!