What is a DELETE API?
- A DELETE API is an HTTP method used to remove a resource from a server.
- It is part of the standard CRUD operations (Create, Read, Update, Delete).
- Example scenario: If your API has a list of users, a DELETE request to /users/5 would remove the user with ID 5.
- DELETE requests usually don’t return a body, but sometimes may return a confirmation message.
- After a DELETE operation, the resource should no longer exist.
- The server typically returns:
- Set the Base URI and Endpoint: Specify the API URL and the resource to delete.
- Send the DELETE Request: Make a DELETE call to the resource endpoint.
- Validate the Response:
- Verify Deletion:
To test a DELETE API using Rest Assured in Java, you follow a standard procedure:
API used for DELETE operation:
DELETE https://reqres.in/api/users/2
2.Steps to Test DELETE API via Rest Assured:
- Set the Base URI
- Specify the Endpoint (example:
/users/2) - Use
given()to prepare request - Use
.when().delete()to send the request - Use
.then()to validate the response (status code, etc.)
Expected Response from https://reqres.in/api/users/2:
The ReqRes API will respond with HTTP status code 204 No Content which indicates successful deletion, but no body is returned.
Maven Dependencies
<dependencies> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20230227</version> </dependency> </dependencies>
import io.restassured.RestAssured; import io.restassured.response.Response; import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class DeleteUserTest { public static void main(String[] args) { // Step 1: Set Base URI RestAssured.baseURI = "https://reqres.in/api"; // Step 2: Perform DELETE request and validate Response response = given() .when() .delete("/users/2") // Endpoint .then() .statusCode(204) // Assert expected status code .extract().response(); // Step 3: Print response for debugging (though it should be empty) System.out.println("Status Code: " + response.getStatusCode()); System.out.println("Response Body: '" + response.getBody().asString() + "'"); } }
statusCode(204)ensures deletion is successful.- The response body should be empty (
""), as per REST standard for DELETE.
Suggested Posts:
1. Validate XML Schema in RestAssured
2. How to extract Response in Rest Assured by JSONPath
3. Test OAth2 in RestAssured
4. Validate Request and Response in POJO by RestAssured
5. How to Test Basic Authentication in RestAssured