Skip to content

Method deleteOrderById throws unexpected exception if order is not found #7

@EgorkaKulikov

Description

@EgorkaKulikov

Consider a scenario when method deleteOrderById is run with an id that is not present in database.

It is expected to get the following response:

ResponseEntity<>(("Order deletion failed - Order ID:" + id), HttpStatus.BAD_REQUEST)

However, this method fails with an exception.

The following test may be used to reproduce a bug:

@SpringBootTest
@AutoConfigureTestDatabase
@AutoConfigureMockMvc
public final class OrderControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    @DisplayName("deleteOrderById: id = 256 (mutated from zero) -> throw NestedServletException")
    public void testDeleteOrderByIdThrowsNSE() throws Exception {
        UriComponentsBuilder uriComponentsBuilder = fromPath("/api/orders/{id}");
        Map uriVariables = new HashMap();
        uriVariables.put("id", 256L);
        UriComponentsBuilder uriComponentsBuilder1 = uriComponentsBuilder.uriVariables(uriVariables);
        String urlTemplate = uriComponentsBuilder1.toUriString();
        Object[] uriVariables1 = {};
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder = delete(urlTemplate, uriVariables1);
        
        /* This test fails because method [org.springframework.test.web.servlet.MockMvc.perform]
            produces [org.springframework.web.util.NestedServletException: Request processing failed;
            nested exception is com.rest.order.exceptions.OrderNotFoundException: Order Not Found with ID: 256]

            com.rest.order.services.OrderService.throwException(OrderService.java:42)
            com.rest.order.services.OrderService.deleteOrderById(OrderService.java:32)
            com.rest.order.controllers.OrderController.deleteOrderById(OrderController.java:37) */
        mockMvc.perform(mockHttpServletRequestBuilder);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions