EST Assured is a Java library for testing RESTful web services. It provides a simple DSL (Domain Specific Language) that makes writing API tests easy, readable, and maintainable.
Here’s an example of how you would use REST Assured to perform CRUD Operations with REST Assured. REST Assured simplifies the process of testing CRUD (Create, Read, Update, Delete) operations, which are fundamental for interacting with most REST APIs. Let’s explore each operation in detail.
1. GET Request
A GET request is used to retrieve data from an API. Here’s how you can test it with REST Assured.
Response response = given()
.baseUri("https://api.example.com/v2")
.when()
.get("/pet/1")
.then()
.statusCode(200)
.extract()
.response();
2. POST Request
A POST request is used to create a new resource. In this example, we are sending a payload to create a new pet.
String requestBody = "{ \"id\": 123, \"name\": \"Buddy\", \"status\": \"available\" }";
given()
.baseUri("https://api.example.com/v2")
.header("Content-Type", "application/json")
.body(requestBody)
.when()
.post("/pet")
.then()
.statusCode(200);
3. PUT Request
PUT is used to update a resource. For instance, changing the pet’s status:
String updatedBody = "{ \"id\": 123, \"name\": \"Buddy\", \"status\": \"sold\" }";
given()
.baseUri("https://api.example.com/v2")
.header("Content-Type", "application/json")
.body(updatedBody)
.when()
.put("/pet")
.then()
.statusCode(200);
4. DELETE Request
To delete a resource, you would use the DELETE request method:
given()
.baseUri("https://api.example.com/v2")
.when()
.delete("/pet/123")
.then()
.statusCode(200);
Combined Example of CRUD Operation using the Rest Assured:
package crud;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
public class CRUDOperationsTest {
// Base URI
private static final String BASE_URL = "https://jsonplaceholder.typicode.com/posts";
/**
* Create (POST Request)
*/
@Test
public void testCreate() {
// JSON request body
String requestBody = """
{
"title": "foo",
"body": "bar",
"userId": 1
}
""";
// Sending POST request
Response response = RestAssured.given()
.header("Content-Type", "application/json")
.body(requestBody)
.when()
.post(BASE_URL);
// Printing and validating the response
System.out.println("Create Response: " + response.getBody().asString());
Assert.assertEquals(response.getStatusCode(), 201, "Status Code Check");
Assert.assertTrue(response.getBody().asString().contains("foo"), "Response Body Check");
}
/**
* Read (GET Request)
*/
@Test
public void testRead() {
// Sending GET request
Response response = RestAssured.given()
.when()
.get(BASE_URL + "/1");
// Printing and validating the response
System.out.println("Read Response: " + response.getBody().asString());
Assert.assertEquals(response.getStatusCode(), 200, "Status Code Check");
Assert.assertTrue(response.getBody().asString().contains("id"), "Response Body Check");
}
/**
* Update (PUT Request)
*/
@Test
public void testUpdate() {
// JSON request body
String requestBody = """
{
"id": 1,
"title": "updated title",
"body": "updated body",
"userId": 1
}
""";
// Sending PUT request
Response response = RestAssured.given()
.header("Content-Type", "application/json")
.body(requestBody)
.when()
.put(BASE_URL + "/1");
// Printing and validating the response
System.out.println("Update Response: " + response.getBody().asString());
Assert.assertEquals(response.getStatusCode(), 200, "Status Code Check");
Assert.assertTrue(response.getBody().asString().contains("updated title"), "Response Body Check");
}
/**
* Delete (DELETE Request)
*/
/**
* Delete (DELETE Request)
*/
@Test
public void testDelete() {
// Sending DELETE request
Response response = RestAssured.given()
.when()
.delete(BASE_URL + "/1");
// Printing the response body
System.out.println("Delete Response: " + response.getBody().asString());
// Validating the response
Assert.assertEquals(response.getStatusCode(), 200, "Status Code Check");
// Checking if the response body is empty or contains an empty JSON
String responseBody = response.getBody().asString();
boolean isResponseEmpty = responseBody.isEmpty() || "{}".equals(responseBody.trim());
Assert.assertTrue(isResponseEmpty, "Response Body Check");
}
}
Output:

REST Assured makes API testing fast, reliable, and maintainable. Whether it’s a simple GET request or complex authentication and payload validation.