Skip to content

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

Closed as not planned

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.

Activity

  1. wilkinsona commented on May 9, 2023

    @wilkinsona
    Member

    @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.

  2. lukg commented on May 9, 2023

    @lukg
    Author

    No problem, I will prepare some minimal project with this issue and dummy fix for it.

  3. wilkinsona commented on May 10, 2023

    @wilkinsona
    Member

    Thanks, @lukg. There's no need for a fix just yet. I'd like to understand the problem first.

  4. lukg commented on May 11, 2023

    @lukg
    Author

    @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.

  5. changed the title [-]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[/+] on May 11, 2023
  6. wilkinsona commented on May 11, 2023

    @wilkinsona
    Member

    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. 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.

  7. wilkinsona commented on Oct 1, 2025

    @wilkinsona
    Member

    Adding support in REST Docs for MockMvcWebTestClient has been superseded by adding support for RestTestClient. As such, I'm going to decline this issue.

  8. added
    status: declinedA suggestion or change that we don't feel we should currently apply
    and removed
    status: blockedAn issue that's blocked on an external project change
    on Oct 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions