1

Is there a way to produce RestDocs with the new (since Boot 3.4) MockMvcTester API? (Not using plain MockMvc?)

I did not find any API on the new fluent style MockMvcTester.

assertThat(
 mockMvc.get()
 .uri("/api/customer/{id}", customer.customerNumber())
 .exchange()
 )
 .hasStatus(HttpStatus.OK)
 .hasHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
 .bodyJson().convertTo(CustomerApiDTO.class)
 .isEqualTo(customer)
 // does not compile anyway
 .consumeWith(document("findByCustomerNumber", // <1>
 pathParameters(
 parameterWithName("id")
 .description("The public customerNumber")
 ),
 responseFields(
 fieldWithPath("customerNumber")
 .description("The unique public ID"),
 fieldWithPath("firstname")
 .description("The first name"),
 fieldWithPath("lastname")
 .description("The last name")
 )
 ));
asked Aug 25, 2024 at 15:16

1 Answer 1

1

You should use apply(ResultHandler) to perform the REST documentation:

assertThat(
 mockMvc.get()
 .uri("/api/customer/{id}", customer.customerNumber())
 .exchange()
)
.hasStatus(HttpStatus.OK)
.hasHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.bodyJson().convertTo(CustomerApiDTO.class)
.isEqualTo(customer)
.apply(document("findByCustomerNumber", // <1>
 pathParameters(
 parameterWithName("id")
 .description("The public customerNumber")
 ),
 responseFields(
 fieldWithPath("customerNumber")
 .description("The unique public ID"),
 fieldWithPath("firstname")
 .description("The first name"),
 fieldWithPath("lastname")
 .description("The last name")
 )
));
answered Sep 5, 2024 at 14:35
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for helping me with that. I could make it compile, but now it fails like: java.lang.IllegalArgumentException: urlTemplate not found. If you are using MockMvc did you use RestDocumentationRequestBuilders to build the request? `
Unfortunately, there's a limitation with documenting path parameters at the moment. It will be addressed by github.com/spring-projects/spring-restdocs/issues/939.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.