Closed
Description
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
Type
Projects
Status
Done