Search⌘ K
AI Features

Solution: The DELETE Operation

Explore how to implement and test secure DELETE operations in a Spring Boot application. Learn to enforce role-based access control using Spring Security, validate API responses with WebTestClient, and verify data changes in MongoDB through reactive testing with StepVerifier.

Here’s the solution to the previous challenge. We’ll discuss it in detail.

deletingInventoryWithoutProperRoleFails() test function

Let’s take a look at the first test function deletingInventoryWithoutProperRoleFails() in the code snippet below:

Java
@Test
@WithMockUser(username = "carol", roles = { "SOME_OTHER_ROLE" }) //1
void deletingInventoryWithoutProperRoleFails() {
this.webTestClient.delete().uri("/some-item") //2
.exchange() //3
.expectStatus().isForbidden(); //4
}

Here’s a breakdown of the code above:

  1. In line 2, the user carol has the SOME_OTHER_ROLE authority. ...