Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lukg opened this issue May 8, 2023 · 5 comments
Open

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

lukg opened this issue May 8, 2023 · 5 comments
Labels
status: blocked An issue that's blocked on an external project change

Comments

@lukg
Copy link

lukg commented May 8, 2023

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 8, 2023
@wilkinsona
Copy link
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.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label May 9, 2023
@lukg
Copy link
Author

lukg commented May 9, 2023

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

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 9, 2023
@scottfrederick scottfrederick added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels May 9, 2023
@wilkinsona
Copy link
Member

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

@lukg
Copy link
Author

lukg commented 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:

  • mockmvc case works
  • webtestclient case works
  • mockmvc + restdocs case works
  • webtestclient + restdosc case fails

after dummy fix all 4 cases works.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 11, 2023
@wilkinsona wilkinsona 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 May 11, 2023
@wilkinsona wilkinsona added type: bug A general bug status: blocked An issue that's blocked on an external project change and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided type: bug A general bug labels May 11, 2023
@wilkinsona
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: blocked An issue that's blocked on an external project change
Projects
None yet
Development

No branches or pull requests

4 participants