|
1 | 1 | package com.baeldung.boot.problem.controller;
|
2 | 2 |
|
| 3 | +import static org.hamcrest.CoreMatchers.containsString; |
3 | 4 | import static org.hamcrest.CoreMatchers.equalTo;
|
| 5 | +import static org.hamcrest.CoreMatchers.notNullValue; |
4 | 6 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
5 | 7 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
6 | 8 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
@@ -71,5 +73,29 @@ public void whenMakeDeleteCall_thenReturnForbiddenProblemResponse() throws Excep
|
71 | 73 | .andExpect(jsonPath("$.detail", equalTo("You can't delete this task")))
|
72 | 74 | .andExpect(status().isForbidden());
|
73 | 75 | }
|
| 76 | + |
| 77 | + @Test |
| 78 | + public void whenMakeGetCallWithInvalidIdFormat_thenReturnBadRequestResponseWithStackTrace() throws Exception { |
| 79 | + mockMvc.perform(get("/tasks/invalid-id").contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE)) |
| 80 | + .andDo(print()) |
| 81 | + .andExpect(jsonPath("$.title", equalTo("Bad Request"))) |
| 82 | + .andExpect(jsonPath("$.status", equalTo(400))) |
| 83 | + .andExpect(jsonPath("$.stacktrace", notNullValue())) |
| 84 | + .andExpect(status().isBadRequest()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void whenMakeGetCallWithInvalidIdFormat_thenReturnBadRequestResponseWithCause() throws Exception { |
| 89 | + mockMvc.perform(get("/tasks/invalid-id").contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE)) |
| 90 | + .andDo(print()) |
| 91 | + .andExpect(jsonPath("$.title", equalTo("Bad Request"))) |
| 92 | + .andExpect(jsonPath("$.status", equalTo(400))) |
| 93 | + .andExpect(jsonPath("$.cause", notNullValue())) |
| 94 | + .andExpect(jsonPath("$.cause.title", equalTo("Internal Server Error"))) |
| 95 | + .andExpect(jsonPath("$.cause.status", equalTo(500))) |
| 96 | + .andExpect(jsonPath("$.cause.detail", containsString("For input string:"))) |
| 97 | + .andExpect(jsonPath("$.cause.stacktrace", notNullValue())) |
| 98 | + .andExpect(status().isBadRequest()); |
| 99 | + } |
74 | 100 |
|
75 | 101 | }
|
0 commit comments