Automating DELETE request using Rest Assured


In the previous post, we have learned about automating PATCH request using Rest Assured. In this Rest Assured tutorial, we will study about sending DELETE request using Rest Assured. I would highly recommend reading previous posts before proceeding with this one.

A DELETE request will delete a resource on the server. Like PUT, this method is rarely permitted on the server for obvious reasons.

The db.json file before sending the DELETE request is a follows:
Now lets look at example of sending of DELETE request

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class DeleteRequestTest {

  @BeforeClass
  public void setBaseUri () {

    RestAssured.baseURI = "https://localhost:3000";
  }


@Test
  public void deleteTest () {
      
    given()
    .when ()
    .contentType (ContentType.JSON)
    .delete ("/posts/3");

  }
}

In the setBaseUri method, We are simply setting the default URI  to use in all of our tests. Now when we write out a test that calls an API (as we will do in the next step), we don’t have to type in the full address of the API. In this case, the baseUri is http://localhost:3000.

In the deleteTest method, we start with Given method. Under When we simply add a check ‘contentType(ContentType.JSON)‘ to make sure that the response we get is in JSON format and under DELETE method we specify the path '/posts/3' where the resources related to id:3 will be deleted.


In the above example, the resources related to id:3 are deleted from db.json file.

In the next post, we will study about checking Response time using Rest Assured.

Comments

Popular posts from this blog

Extracting an XML Response with Rest-Assured