REST Assured is a popular Java library used for API Automation Testing. It provides an easy way to test RESTful web services and validate responses using a simple and readable syntax.
1. What is REST Assured?
REST Assured is an open-source Java library specifically designed for testing REST APIs. It helps automate API validation without writing complex HTTP client code.
Key Features
- Easy API request creation
- JSON and XML validation support
- BDD-style syntax
- Integration with TestNG and JUnit
- Authentication support
- Schema validation
Example
given()
.when()
.get("https://api.example.com/users")
.then()
.statusCode(200);
2. API Testing Fundamentals
API Testing ensures that APIs work correctly, return expected data, handle errors properly, and meet performance requirements.
Benefits of API Testing
- Faster than UI testing
- Early defect detection
- Better backend validation
- Improved software quality
- Reduced testing costs
What Should Be Validated?
- Status Codes
- Response Body
- Response Headers
- Response Time
- Authentication
- Error Handling
3. HTTP Methods
GET
Used to retrieve data from the server.
given()
.when()
.get("/users")
.then()
.statusCode(200);
POST
Used to create a new resource.
String payload = "{ \"name\": \"John\" }";
given()
.body(payload)
.when()
.post("/users")
.then()
.statusCode(201);
PUT
Used to completely update an existing resource.
given()
.body(updatedPayload)
.when()
.put("/users/1")
.then()
.statusCode(200);
PATCH
Used to partially update a resource.
given()
.body(partialPayload)
.when()
.patch("/users/1")
.then()
.statusCode(200);
DELETE
Used to remove a resource.
given()
.when()
.delete("/users/1")
.then()
.statusCode(204);
4. Request-Response Lifecycle
The API communication process follows these steps:
- Client sends HTTP Request
- Server receives the request
- Business logic executes
- Database interaction occurs
- Server generates response
- Client validates response
Client
|
HTTP Request
|
Server
|
Business Logic
|
Database
|
HTTP Response
|
Client
5. Maven Setup
Maven is a build automation and dependency management tool for Java projects.
Steps to Create a Maven Project
- Open IntelliJ IDEA or Eclipse
- Create a New Maven Project
- Provide GroupId and ArtifactId
- Generate Project Structure
Project Structure
project-name
│
├── src
│ ├── main
│ └── test
│
├── pom.xml
└── target
6. Adding REST Assured Dependencies
Add the following dependencies in the pom.xml file:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
Verification Test
import static io.restassured.RestAssured.*;
@Test
public void verifyAPI() {
given()
.when()
.get("https://reqres.in/api/users/2")
.then()
.statusCode(200);
}
Conclusion
REST Assured simplifies API automation testing in Java. Understanding API fundamentals, HTTP methods, request-response lifecycle, Maven setup, and dependency management creates a strong foundation for advanced REST Assured topics.