Repository navigation
@AutoConfigureRestDocs does not work with a MockMvc-backed WebTestClient #35348
Copy link
Copy link
Closed as not planned
Closed as not planned
Copy link
Labels
status: declinedA suggestion or change that we don't feel we should currently applyA suggestion or change that we don't feel we should currently apply
Description
Currently this is impossible to use autoconfigured WebTestClient with autoconfigured RestDocs together because of this error:
java.lang.IllegalStateException: REST Docs configuration not found. Did you forget to register a WebTestClientRestDocumentationConfigurer as a filter?
at org.springframework.util.Assert.state(Assert.java:97)
at org.springframework.restdocs.webtestclient.WebTestClientRestDocumentationConfigurer.retrieveConfiguration(WebTestClientRestDocumentationConfigurer.java:83)
at org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.retrieveConfiguration(WebTestClientRestDocumentation.java:137)
at org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.lambda$document$0(WebTestClientRestDocumentation.java:77)
at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodyContentSpec.lambda$consumeWith$3(DefaultWebTestClient.java:709)
at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:234)
at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodyContentSpec.consumeWith(DefaultWebTestClient.java:709)
But there is possible to use WebTestClient together with MockMvc (for non full reactive app) - both can be injected in the same time.
The error is raised because of too restrictive autoconfiguration for RestDocs from org.springframework.boot.test.autoconfigure.restdocs.RestDocsAutoConfiguration.
I pasted this configuration below:
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(WebTestClientRestDocumentation.class)
@ConditionalOnWebApplication(type = Type.REACTIVE)
@EnableConfigurationProperties(RestDocsProperties.class)
static class RestDocsWebTestClientConfiguration {
@Bean
@ConditionalOnMissingBean
WebTestClientRestDocumentationConfigurer restDocsWebTestClientConfigurer(
ObjectProvider<RestDocsWebTestClientConfigurationCustomizer> configurationCustomizers,
RestDocumentationContextProvider contextProvider) {
WebTestClientRestDocumentationConfigurer configurer = WebTestClientRestDocumentation
.documentationConfiguration(contextProvider);
configurationCustomizers.orderedStream()
.forEach((configurationCustomizer) -> configurationCustomizer.customize(configurer));
return configurer;
}
@Bean
RestDocsWebTestClientBuilderCustomizer restDocumentationConfigurer(RestDocsProperties properties,
WebTestClientRestDocumentationConfigurer configurer) {
return new RestDocsWebTestClientBuilderCustomizer(properties, configurer);
}
}
What could be changed to improve this: replace @ConditionalOnWebApplication(type = Type.REACTIVE) with something smarter what could detect if the WebTestClient is also in use and than apply automatically RestDocsWebTestClientConfiguration.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
status: declinedA suggestion or change that we don't feel we should currently applyA suggestion or change that we don't feel we should currently apply
Activity
wilkinsona commentedon May 9, 2023
@lukg I'm not sure I've fully understood what you're trying to do but we have an integration test for what I think you've described so I'm not sure why it apparently isn't working for you.
If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
lukg commentedon May 9, 2023
No problem, I will prepare some minimal project with this issue and dummy fix for it.
wilkinsona commentedon May 10, 2023
Thanks, @lukg. There's no need for a fix just yet. I'd like to understand the problem first.
lukg commentedon May 11, 2023
@wilkinsona here is the repo link,
I provided 2 identical test classes: one with dummy fix one without.
Additional thing: the test need to be fully integrational, and by by default:
after dummy fix all 4 cases works.
[-]java.lang.IllegalStateException: REST Docs configuration not found. Did you forget to register a WebTestClientRestDocumentationConfigurer as a filter?[/-][+]@AutoConfigureRestDocs does not work with a MockMvc-backed WebTestClient[/+]wilkinsona commentedon May 11, 2023
Thanks for the sample, @lukg. I now understand what you're trying to do.
I had missed that fact that you were using
WebTestClientbacked byMockMvc. REST Docs doesn't officially support that combination at the moment. spring-projects/spring-restdocs#691 is tracking whatever work may be necessary to add support. At the least, some documentation updates are required. It may be that no code changes in REST Docs are required. Until we've had time to figure that out, there's nothing that we can do in Spring Boot. As such, I've marked this issue as blocked.wilkinsona commentedon Oct 1, 2025
Adding support in REST Docs for
MockMvcWebTestClienthas been superseded by adding support forRestTestClient. As such, I'm going to decline this issue.