Skip to content

Display exception message and stack trace in controller-specific integration tests #2586

Closed
@IlyaMuravjov

Description

@IlyaMuravjov

Description

Controller tests causing user code to throw an exception do not show exception message, nor stack trace and only show that NestedServletException was thrown, which is not particularly informative.

For example, the following test is generated for OrderController.deleteOrderById() in spring-boot-testing project

@Test
@DisplayName("deleteOrderById: id = -1 (mutated from max) -> throw NestedServletException")
public void testDeleteOrderByIdThrowsNSE() {
    UriComponentsBuilder uriComponentsBuilder = fromPath("/api/orders/{id}");
    Map uriVariables = new HashMap();
    uriVariables.put("id", -1L);
    UriComponentsBuilder uriComponentsBuilder1 = uriComponentsBuilder.uriVariables(uriVariables);
    String urlTemplate = uriComponentsBuilder1.toUriString();
    Object[] uriVariables1 = {};
    MockHttpServletRequestBuilder mockHttpServletRequestBuilder = delete(urlTemplate, uriVariables1);

    assertThrows(NestedServletException.class, () -> mockMvc.perform(mockHttpServletRequestBuilder));
}

Expected behavior

There should be exception message and stack trace rendered

@Test
@DisplayName("deleteOrderById: id = -1 (mutated from max) -> throw NestedServletException")
public void testDeleteOrderByIdThrowsNSE() throws Exception {
    UriComponentsBuilder uriComponentsBuilder = fromPath("/api/orders/{id}");
    Map uriVariables = new HashMap();
    uriVariables.put("id", -1L);
    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: -1]
        com.rest.order.services.SimpleOrderService.throwException(SimpleOrderService.java:114)
        com.rest.order.services.SimpleOrderService.deleteOrderById(SimpleOrderService.java:69)
        com.rest.order.controllers.OrderController.deleteOrderById(OrderController.java:60) */
    mockMvc.perform(mockHttpServletRequestBuilder);
}

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorcomp-springIssue is related to Spring projects supportctg-enhancementNew feature, improvement or change request

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions