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")
)
));
1 Answer 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
Andy Wilkinson
118k25 gold badges279 silver badges259 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Eiswind
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? `
Andy Wilkinson
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.