Skip to content

@AutoConfigureRestDocs does not work with a MockMvc-backed WebTestClient #35348

Open
@lukg

Description

@lukg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: blockedAn issue that's blocked on an external project change

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions