In Spring Boot 2.1.3, the RestDocsWebTestClientConfiguration
class is annotated with @ConditionalOnWebApplication(type = Type.REACTIVE)
.
This, as far as I understand, prevents the usage of an autoconfigured WebTestClient to document an MVC app, which is strange since you can use WebTestClient to test an MVC app.
Comment From: wilkinsona
since you can use WebTestClient to test an MVC app.
My understanding is that you can only use WebTestClient
to test an MVC app if you start an embedded server and use HTTP. In other words, using mocked requests and responses is only supported when testing a WebFlux app.
Given a choice between testing an MVC app using MockMvc
with no need for a server and using WebTestClient
and requiring an embedded container, I would choose the former. I also think that's what we should encourage users to do, hence things being the way they are just now. Perhaps I'm unusual and we should leave the door open to using WebTestClient
with HTTP to test an MVC app.
@jnizet Does my understanding of the situation match yours? Is your preference to use WebTestClient
over HTTP to test your MVC app?
Comment From: jnizet
Sure, sorry for the noise.
I'm not very experienced with WebFlux, and missed the WebFluxTest annotation. So I had the feeling that WebTestClient was always used to create integration tests with an embedded server and HTTP, and thus that using MVC or WebFlux wouldn't make a difference.
Now I'm starting to realize that the AutoConfigureRestDocs annotation is usable
- in a
@WebMvcTest
-annotated test, to test an MVC controller using a mock MVC context, with the help of au autowired MockMvc - in a
@WebFluxTest
-annotated test, to test a Webflux controller using a mock WebFlux context, with the help of an autowired WebTestClient - in a test based on RestAssured, to test an app over HTTP, in order to automatically provide an autowirable documentation spec to the test
WebTestClient
can be also be used in @SpringBootTest
tests to test an app over HTTP, but then AutoConfigureRestDocs is of no help: the WebTestClient must be configured explicitly, as shown in the Spring REST docs user guide.
To answer your final question: no, my preference is to use sliced tests, using MockMvc, to test and document my MVC apps. But if I had to write actual integration tests, testing the actual app over HTTP, I guess I would use WebTestClient.
Comment From: wilkinsona
Thanks. Let's use this issue to improve the documentation. It sounds like we need to make it clearer when @AutoConfigureRestDocs
can be used and when you'd want to configure the WebTestClient
yourself.