How to document @ControllerAdvice handled exception using Spring REST Docs
up vote
0
down vote
favorite
I have @ControllerAdvice
annotated class, which is handling BadRequestException extends RuntimeException
exception.
Now suppose that I have endpoint:
@PostMapping(value = "/createAccount")
public ResponseEntity<CreateAccountResponse> createAccount(@RequestBody @Valid CreateAccountRequest createAccountRequest) ...
In case of unwanted scenario, endpoint throws BadRequestException
(with HTTP Status 400) which constructs error JSON object as following:
"errorCode": 123,
"errorMessage: "Failure reason"
Is there any way to document case like this using Spring REST Docs ?
This is example of my approach:
@Test
public void createAccountFailExample()
RestDocumentationResultHandler docs = document("create-acc-fail-example",
responseFields(
fieldWithPath("errorCode").type("Integer").description("Error code"),
fieldWithPath("errorMessage").type("String").description("Error message")
)
);
org.assertj.core.api.Assertions.assertThatThrownBy(() -> this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
.contextPath("/account")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("nameTest", "surnameTest"))))
.andExpect(status().isBadRequest())
.andDo(docs)).hasCause(new BadRequestException(ServiceError.SOME_FAIL_REASON));
In this case test passes, but no documentation (.adoc) files are created.
When I try something like this:
ResultActions resultActions = this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
.contextPath("/account")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("testName", "testSurname"))))
.andExpect(status().isBadRequest())
.andDo(docs);
test fails because NestedServletException
was thrown caused by BadRequestException
, and again there is no documentation created.
java spring testing documentation spring-restdocs
add a comment |
up vote
0
down vote
favorite
I have @ControllerAdvice
annotated class, which is handling BadRequestException extends RuntimeException
exception.
Now suppose that I have endpoint:
@PostMapping(value = "/createAccount")
public ResponseEntity<CreateAccountResponse> createAccount(@RequestBody @Valid CreateAccountRequest createAccountRequest) ...
In case of unwanted scenario, endpoint throws BadRequestException
(with HTTP Status 400) which constructs error JSON object as following:
"errorCode": 123,
"errorMessage: "Failure reason"
Is there any way to document case like this using Spring REST Docs ?
This is example of my approach:
@Test
public void createAccountFailExample()
RestDocumentationResultHandler docs = document("create-acc-fail-example",
responseFields(
fieldWithPath("errorCode").type("Integer").description("Error code"),
fieldWithPath("errorMessage").type("String").description("Error message")
)
);
org.assertj.core.api.Assertions.assertThatThrownBy(() -> this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
.contextPath("/account")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("nameTest", "surnameTest"))))
.andExpect(status().isBadRequest())
.andDo(docs)).hasCause(new BadRequestException(ServiceError.SOME_FAIL_REASON));
In this case test passes, but no documentation (.adoc) files are created.
When I try something like this:
ResultActions resultActions = this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
.contextPath("/account")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("testName", "testSurname"))))
.andExpect(status().isBadRequest())
.andDo(docs);
test fails because NestedServletException
was thrown caused by BadRequestException
, and again there is no documentation created.
java spring testing documentation spring-restdocs
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have @ControllerAdvice
annotated class, which is handling BadRequestException extends RuntimeException
exception.
Now suppose that I have endpoint:
@PostMapping(value = "/createAccount")
public ResponseEntity<CreateAccountResponse> createAccount(@RequestBody @Valid CreateAccountRequest createAccountRequest) ...
In case of unwanted scenario, endpoint throws BadRequestException
(with HTTP Status 400) which constructs error JSON object as following:
"errorCode": 123,
"errorMessage: "Failure reason"
Is there any way to document case like this using Spring REST Docs ?
This is example of my approach:
@Test
public void createAccountFailExample()
RestDocumentationResultHandler docs = document("create-acc-fail-example",
responseFields(
fieldWithPath("errorCode").type("Integer").description("Error code"),
fieldWithPath("errorMessage").type("String").description("Error message")
)
);
org.assertj.core.api.Assertions.assertThatThrownBy(() -> this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
.contextPath("/account")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("nameTest", "surnameTest"))))
.andExpect(status().isBadRequest())
.andDo(docs)).hasCause(new BadRequestException(ServiceError.SOME_FAIL_REASON));
In this case test passes, but no documentation (.adoc) files are created.
When I try something like this:
ResultActions resultActions = this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
.contextPath("/account")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("testName", "testSurname"))))
.andExpect(status().isBadRequest())
.andDo(docs);
test fails because NestedServletException
was thrown caused by BadRequestException
, and again there is no documentation created.
java spring testing documentation spring-restdocs
I have @ControllerAdvice
annotated class, which is handling BadRequestException extends RuntimeException
exception.
Now suppose that I have endpoint:
@PostMapping(value = "/createAccount")
public ResponseEntity<CreateAccountResponse> createAccount(@RequestBody @Valid CreateAccountRequest createAccountRequest) ...
In case of unwanted scenario, endpoint throws BadRequestException
(with HTTP Status 400) which constructs error JSON object as following:
"errorCode": 123,
"errorMessage: "Failure reason"
Is there any way to document case like this using Spring REST Docs ?
This is example of my approach:
@Test
public void createAccountFailExample()
RestDocumentationResultHandler docs = document("create-acc-fail-example",
responseFields(
fieldWithPath("errorCode").type("Integer").description("Error code"),
fieldWithPath("errorMessage").type("String").description("Error message")
)
);
org.assertj.core.api.Assertions.assertThatThrownBy(() -> this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
.contextPath("/account")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("nameTest", "surnameTest"))))
.andExpect(status().isBadRequest())
.andDo(docs)).hasCause(new BadRequestException(ServiceError.SOME_FAIL_REASON));
In this case test passes, but no documentation (.adoc) files are created.
When I try something like this:
ResultActions resultActions = this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
.contextPath("/account")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("testName", "testSurname"))))
.andExpect(status().isBadRequest())
.andDo(docs);
test fails because NestedServletException
was thrown caused by BadRequestException
, and again there is no documentation created.
java spring testing documentation spring-restdocs
java spring testing documentation spring-restdocs
asked Nov 10 at 13:13
hideburn
85112
85112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I managed to solve the problem following this answer. When exception handler is defined for MockMvc
, my second approach works as expected.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I managed to solve the problem following this answer. When exception handler is defined for MockMvc
, my second approach works as expected.
add a comment |
up vote
0
down vote
I managed to solve the problem following this answer. When exception handler is defined for MockMvc
, my second approach works as expected.
add a comment |
up vote
0
down vote
up vote
0
down vote
I managed to solve the problem following this answer. When exception handler is defined for MockMvc
, my second approach works as expected.
I managed to solve the problem following this answer. When exception handler is defined for MockMvc
, my second approach works as expected.
answered Nov 11 at 13:57
hideburn
85112
85112
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239293%2fhow-to-document-controlleradvice-handled-exception-using-spring-rest-docs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown