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.
Comment From: wilkinsona
@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.
Comment From: lukg
No problem, I will prepare some minimal project with this issue and dummy fix for it.
Comment From: wilkinsona
Thanks, @lukg. There's no need for a fix just yet. I'd like to understand the problem first.
Comment From: lukg
@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:
- mockmvc case works
- webtestclient case works
- mockmvc + restdocs case works
- webtestclient + restdosc case fails
after dummy fix all 4 cases works.
Comment From: wilkinsona
Thanks for the sample, @lukg. I now understand what you're trying to do.
I had missed that fact that you were using WebTestClient backed by MockMvc. REST Docs doesn't officially support that combination at the moment. https://github.com/spring-projects/spring-restdocs/issues/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.