How to test DELETE in Rest Assured

HOME

In the last tutorial, I have explained How to test PUT Request using Rest Assured. In this tutorial, I will automate a DELETE Request using Rest Assured. I will verify the status code, line of Status, and content of the Response.

To setup a basic Rest Assured Maven Project, click here and Gradle project, click here.

What is DELETE Method?

An HTTP DELETE method is used to delete an existing resource from collection of resources. The DELETE method requests the origin server to delete the resource identified by the Request-URI. On successful deletion of resource, it returns  200 (OK) and 204 (No Content) status code. It may return as 202 (Accepted) status code if request is queued. . To know more about Rest API, please click here.

Below are the steps to test a DELETE Request using Rest Assured:

The steps to test the DELETE request is similar to any API request, like GET, POST, PUT. To know about the steps and various imports used in the below example in detail, please refer to the tutorial for POST Request.

Let’s see existing details of a Employee ID 3 using Postman:

Let’s write DELETE request in REST Assured in Non BDD Format for id 3:

public class Delete_NonBddDemo {

	RequestSpecification requestSpecification;
	Response response;
	ValidatableResponse validatableResponse;

	@Test
	public void deleteUser() {

		RestAssured.baseURI = "http://dummy.restapiexample.com/api";

		// Create a request specification
		requestSpecification = RestAssured.given();

		// Calling DELETE method
		response = requestSpecification.delete("/v1/delete/3");

		// Let's print response body.
		String resString = response.prettyPrint();

		/*
		 * To perform validation on response, we need to get ValidatableResponse type of
		 * response
		 */
		validatableResponse = response.then();

		// Get status code
		validatableResponse.statusCode(200);

		// It will check if status line is as expected
		validatableResponse.statusLine("HTTP/1.1 200 OK");

		// Check response - message attribute
		validatableResponse.body("message", equalTo("Successfully! Record has been deleted"));

	}
}

Let’s write DELETE request in REST Assured in BDD Format:

public class Delete_BDDDemo {

	ValidatableResponse validatableResponse;

	@Test
	public void deleteUser() {

		
	validatableResponse = given()
                                .baseUri("http://dummy.restapiexample.com/api/v1/delete/3")
				                .contentType(ContentType.JSON)
                         .when()
                                .delete()
				         .then()
                                .assertThat().statusCode(200)
                                .body("message", equalTo("Successfully! Record has been deleted"));

		System.out.println("Response :" + validatableResponse.extract().asPrettyString());

	}

}

We are done. Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s