Enable stacktraces and causal chain of causes. Add a couple of tests to validate stacktrace and cause are included in responses (#6315)
This commit is contained in:
parent
f894b40fe6
commit
e2f3d605f0
|
@ -5,5 +5,12 @@ import org.zalando.problem.spring.web.advice.ProblemHandling;
|
|||
|
||||
@ControllerAdvice
|
||||
public class ExceptionHandler implements ProblemHandling {
|
||||
|
||||
// The causal chain of causes is disabled by default,
|
||||
// but we can easily enable it by overriding the behavior:
|
||||
@Override
|
||||
public boolean isCausalChainsEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,6 +12,8 @@ public class ProblemDemoConfiguration {
|
|||
|
||||
@Bean
|
||||
public ObjectMapper objectMapper() {
|
||||
return new ObjectMapper().registerModules(new ProblemModule(), new ConstraintViolationProblemModule());
|
||||
// In this example, stack traces support is enabled by default.
|
||||
// If you want to disable stack traces just use new ProblemModule() instead of new ProblemModule().withStackTraces()
|
||||
return new ObjectMapper().registerModules(new ProblemModule().withStackTraces(), new ConstraintViolationProblemModule());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.baeldung.boot.problem.controller;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
|
@ -71,5 +73,29 @@ public class ProblemDemoControllerIntegrationTest {
|
|||
.andExpect(jsonPath("$.detail", equalTo("You can't delete this task")))
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMakeGetCallWithInvalidIdFormat_thenReturnBadRequestResponseWithStackTrace() throws Exception {
|
||||
mockMvc.perform(get("/tasks/invalid-id").contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE))
|
||||
.andDo(print())
|
||||
.andExpect(jsonPath("$.title", equalTo("Bad Request")))
|
||||
.andExpect(jsonPath("$.status", equalTo(400)))
|
||||
.andExpect(jsonPath("$.stacktrace", notNullValue()))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMakeGetCallWithInvalidIdFormat_thenReturnBadRequestResponseWithCause() throws Exception {
|
||||
mockMvc.perform(get("/tasks/invalid-id").contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE))
|
||||
.andDo(print())
|
||||
.andExpect(jsonPath("$.title", equalTo("Bad Request")))
|
||||
.andExpect(jsonPath("$.status", equalTo(400)))
|
||||
.andExpect(jsonPath("$.cause", notNullValue()))
|
||||
.andExpect(jsonPath("$.cause.title", equalTo("Internal Server Error")))
|
||||
.andExpect(jsonPath("$.cause.status", equalTo(500)))
|
||||
.andExpect(jsonPath("$.cause.detail", containsString("For input string:")))
|
||||
.andExpect(jsonPath("$.cause.stacktrace", notNullValue()))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue