In the previous tutorial, I explained about the Testing of SpringBoot PUT Method. In this tutorial, I will discuss about the Testing of DELETE method to delete a Resource in SpringBoot application.
To know how you can test a SpringBoot Application in BDD format using Serenity, refer the tutorial related to Serenity BDD with Cucumber for SpringBoot application.
The structure of project and various Java classes present in SpringBoot application and the dependencies need in POM.xml to test springboot framework are mentioned here.
Below is the code of Student Controller which exposes the services of DELETE method of Student resource.
@DeleteMapping("/students/{id}")
@DeleteMapping annotation maps HTTP DELETE requests onto specific handler methods. It is a composed annotation that acts as a shortcut for @RequestMapping(method=RequestMethod.DELETE) .
Code of StudentController.java for DELETE method is below
@DeleteMapping("/students/{id}")
public void deleteStudent(@PathVariable long id) {
studentRepository.deleteById(id);
}
Here, we are deleting the student resource by Id.
Scenario 1- Below picture shows how we can execute a sucessful DELETE Request Method using Postman
Before Deleting a Student Resource with Id 1001
In the below image, it shows all the details of student with Id 1001 and with status code of 201 is returned.

Now, we delete a Student with Id 1001 and the status code returned is 200.

After deleting the resource, again send a request to get the details of student of id 1001 which returns 404 – Not Found status.

Above scenario can be tested in the below way.
Feature: Delete Student Request
@DeleteStudent
Scenario: Send a valid Request to delete a student
Given I send a request to the URL "/students/1001" to get the detail of user with Id 1001
When I send a request to the URL "/students/1001" to delete user
Then the response will return status of 200
And I resend the request to the URL "/students/1001" to get status of 404
Test Code to test above scenario (StepDefinition file)
@SpringBootTest(classes = SpringBoot2RestServiceApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class DeleteStudentsDefinition {
private final static String BASE_URI = "http://localhost";
@LocalServerPort
private int port;
private ValidatableResponse validatableResponse1, validatableResponse2, validatableResponse3;
private void configureRestAssured() {
RestAssured.baseURI = BASE_URI;
RestAssured.port = port;
}
protected RequestSpecification getAnonymousRequest() throws NoSuchAlgorithmException {
configureRestAssured();
return given();
}
@Given("^I send a request to the URL \"([^\"]*)\" to get the detail of user with Id 1001$")
public void getRequest(String endpoint) throws Throwable {
validatableResponse1 = getAnonymousRequest().contentType(ContentType.JSON).body(toString()).when().get(endpoint)
.then();
}
@When("^I send a request to the URL \"([^\"]*)\" to delete user$")
public void iSendARequest(String endpoint) throws Throwable {
validatableResponse2 = getAnonymousRequest().contentType(ContentType.JSON).when().delete(endpoint).then();
}
@Then("^the response will return status of (\\d+)$")
public void extractResponseOfValidStudent(int status) throws NoSuchAlgorithmException {
validatableResponse2.assertThat().statusCode(equalTo(status));
}
@And("^I resend the request to the URL \"([^\"]*)\" to get status of (\\d+)$")
public void reverifyStudent(String endpoint, int status) throws NoSuchAlgorithmException {
validatableResponse3 = getAnonymousRequest().contentType(ContentType.JSON).body(toString()).when().get(endpoint)
.then();
validatableResponse3.assertThat().statusCode(equalTo(status));
}
}

We can test the negative scenario similarly. Send a request with invalid student id, then we will get 500 Internal Server Error.

The next tutorial explains about the Testing of SpringBoot Exception Handling.