Slide 68
Slide 68 text
Central Info - Errors
void 'test and document error format’() {
when:
ResultActions result = this.mockMvc.perform(put('/error')
.contentType(MediaType.APPLICATION_JSON)
.requestAttr(RequestDispatcher.ERROR_STATUS_CODE, 405)
.requestAttr(RequestDispatcher.ERROR_REQUEST_URI, '/hello')
.requestAttr(RequestDispatcher.ERROR_MESSAGE,
"Request method 'PUT' not supported"))
then:
result
.andExpect(status().isMethodNotAllowed())
.andDo(document('error-example', preprocessResponse(prettyPrint()),
responseFields(
fieldWithPath(‘error')
.description('The HTTP error that occurred, e.g. `Bad Request`'),
fieldWithPath(‘message')
.description('A description of the cause of the error'),
fieldWithPath('path').description('The path to which the request was made'),
fieldWithPath('status').description('The HTTP status code, e.g. `400`'),
fieldWithPath(‘timestamp')
.description('The time, in milliseconds, at which the error occurred'))
))
}