API Automation with the RestAssured Framework in Java provides a powerful way to test RESTful web services. Here’s a structured guide to setting up and writing tests using RestAssured:
1. Project Setup
Maven Dependencies
Add the required dependencies to your pom.xml
:
<dependencies>
<!-- RestAssured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.x.x</version>
<scope>test</scope>
</dependency>
<!-- JSON Path for parsing -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>5.x.x</version>
</dependency>
<!-- TestNG or JUnit -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.x.x</version>
<scope>test</scope>
</dependency>
</dependencies>
2. Key Concepts in RestAssured
- BaseURI: The base URL of your API (e.g.,
https://api.example.com
). - Request Specification: Common configurations like headers, auth, etc.
- Response Validation: Validate status codes, headers, and response bodies.
- JSON Path: Extract data from JSON responses.
- Logging: Log requests and responses for debugging.
3. Basic Test Example
Test: Validate a GET Request
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
public class GetRequestTest {
@Test
public void validateGetRequest() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
Response response = given()
.header("Content-Type", "application/json")
.when()
.get("/posts/1")
.then()
.statusCode(200)
.log().all()
.extract().response();
// Validate response body
String title = response.jsonPath().getString("title");
Assert.assertNotNull(title, "Title should not be null");
}
}
4. POST Request Test
Test: Create a New Resource
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class PostRequestTest {
@Test
public void validatePostRequest() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
String requestBody = "{\n" +
" \"title\": \"foo\",\n" +
" \"body\": \"bar\",\n" +
" \"userId\": 1\n" +
"}";
given()
.header("Content-Type", "application/json")
.body(requestBody)
.when()
.post("/posts")
.then()
.statusCode(201)
.body("title", equalTo("foo"))
.log().all();
}
}
5. PUT Request Test
Test: Update an Existing Resource
@Test
public void validatePutRequest() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
String requestBody = "{\n" +
" \"title\": \"updatedTitle\",\n" +
" \"body\": \"updatedBody\",\n" +
" \"userId\": 1\n" +
"}";
given()
.header("Content-Type", "application/json")
.body(requestBody)
.when()
.put("/posts/1")
.then()
.statusCode(200)
.body("title", equalTo("updatedTitle"))
.log().all();
}
6. DELETE Request Test
Test: Delete a Resource
@Test
public void validateDeleteRequest() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
given()
.when()
.delete("/posts/1")
.then()
.statusCode(200)
.log().all();
}
7. Framework Enhancements
Base Test Class
Create a base test class to define standard configurations:
import io.restassured.RestAssured;
import org.testng.annotations.BeforeClass;
public class BaseTest {
@BeforeClass
public void setUp() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
}
}
Request Specification
Centralize reusable request specifications:
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
public class RequestSpec {
public static RequestSpecification getRequestSpec() {
return new RequestSpecBuilder()
.setBaseUri("https://jsonplaceholder.typicode.com")
.addHeader("Content-Type", "application/json")
.build();
}
}
Use it in tests:
given()
.spec(RequestSpec.getRequestSpec())
.when()
.get("/posts/1")
.then()
.statusCode(200);
8. JSON Path for Response Validation
Extract and validate specific data:
Response response = given()
.when()
.get("/posts/1")
.then()
.extract().response();
String title = response.jsonPath().getString("title");
int userId = response.jsonPath().getInt("userId");
Assert.assertEquals(userId, 1, "User ID should be 1");
9. Parameterized Tests
Use query or path parameters:
@Test
public void validateQueryParams() {
given()
.queryParam("userId", 1)
.when()
.get("/posts")
.then()
.statusCode(200)
.body("size()", greaterThan(0))
.log().all();
}
10. Run Tests
- Use TestNG or JUnit as your test runner.
- Run from an IDE or via Maven command:
mvn test
This framework setup ensures scalability, modularity, and reusability. Let me know if you need enhancements like integration with reporting tools or CI/CD!