|
16 | 16 | import io.github.bsayli.customerservice.testconfig.TestControllerMocksConfig; |
17 | 17 | import java.util.List; |
18 | 18 | import java.util.NoSuchElementException; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
19 | 20 | import org.junit.jupiter.api.DisplayName; |
20 | 21 | import org.junit.jupiter.api.Tag; |
21 | 22 | import org.junit.jupiter.api.Test; |
| 23 | +import org.mockito.Mockito; |
22 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
23 | 25 | import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
24 | 26 | import org.springframework.context.annotation.Import; |
@@ -161,4 +163,50 @@ void deleteCustomer_ok200() throws Exception { |
161 | 163 | .andExpect(jsonPath("$.data.customerId").value(1)) |
162 | 164 | .andExpect(jsonPath("$.data.deletedAt").exists()); |
163 | 165 | } |
| 166 | + |
| 167 | + @Test |
| 168 | + @DisplayName("POST /v1/customers -> 400 Bad Request when JSON is malformed") |
| 169 | + void createCustomer_badJson_notReadable() throws Exception { |
| 170 | + var malformed = "{ \"name\": \"John\", \"email\": }"; // invalid JSON |
| 171 | + |
| 172 | + mvc.perform( |
| 173 | + post("/v1/customers") |
| 174 | + .contentType(MediaType.APPLICATION_JSON) |
| 175 | + .content(malformed)) |
| 176 | + .andExpect(status().isBadRequest()) |
| 177 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
| 178 | + .andExpect(jsonPath("$.message").value("BAD_REQUEST")) |
| 179 | + .andExpect(jsonPath("$.data.code").value("VALIDATION_FAILED")) |
| 180 | + .andExpect(jsonPath("$.data.violations").isArray()); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + @DisplayName("GET /v1/customers/{id} -> 400 Bad Request on @Min violation (id=0)") |
| 185 | + void getCustomer_constraintViolation_min() throws Exception { |
| 186 | + mvc.perform(get("/v1/customers/{id}", 0)) // @Min(1) violated |
| 187 | + .andExpect(status().isBadRequest()) |
| 188 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
| 189 | + .andExpect(jsonPath("$.message").value("BAD_REQUEST")) |
| 190 | + .andExpect(jsonPath("$.data.code").value("VALIDATION_FAILED")) |
| 191 | + .andExpect(jsonPath("$.data.violations").isArray()) |
| 192 | + .andExpect(jsonPath("$.data.violations[0].message").exists()); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + @DisplayName("GET /v1/customers/{id} -> 500 Internal Server Error handled by advice") |
| 197 | + void getCustomer_internalServerError_generic() throws Exception { |
| 198 | + when(customerService.getCustomer(1)).thenThrow(new RuntimeException("Boom")); |
| 199 | + |
| 200 | + mvc.perform(get("/v1/customers/{id}", 1)) |
| 201 | + .andExpect(status().isInternalServerError()) |
| 202 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
| 203 | + .andExpect(jsonPath("$.message").value("INTERNAL_ERROR")) |
| 204 | + .andExpect(jsonPath("$.data.code").value("INTERNAL_ERROR")) |
| 205 | + .andExpect(jsonPath("$.data.message").value("Boom")); |
| 206 | + } |
| 207 | + |
| 208 | + @AfterEach |
| 209 | + void resetMocks() { |
| 210 | + Mockito.reset(customerService); |
| 211 | + } |
164 | 212 | } |
0 commit comments