Spring Framework recently added WebTestClient
support for testing Spring MVC applications using MockMvc
(see spring-projects/spring-framework#19647).
Right now, Spring Boot offers two ways to test a web application: using TestRestTemplate
on a running web server or with MockMvc
(no actual server involved).
We should consider MockMvcTestClient
and how we should update our testing infrastructure and documentation with this new option.
Comment From: Sam-Kruglov
I can see there is already a MockMvcWebClientAutoConfiguration
, why not create a similar MockMvcWebTestClientAutoConfiguration
?
Here's how I'm doing it and it seems to work fine:
@BeforeEach
void setUp(@Autowired MockMvc mockMvc) {
webTestClient = MockMvcWebTestClient.bindTo(mockMvc).build();
}
1 note: ParameterNamesModule
missing from the default Jackson2JsonDecoder
. Could use ObjectMapper#findAndRegisterModules
Comment From: nightswimmings
@philwebb Does this mean we can use @AutoConfigureMockRestServiceServer with WebTestClient as well, so internal WebClient/RestTemplate calls can be mocked from the test? If so we could update documentation and also okhttp would not be needed anymore as explained here right? https://github.com/spring-projects/spring-framework/issues/19852
Comment From: bclozel
MockRestServiceServer
is not related to MockMvc and it only binds to RestTemplate
.
Even if it did, we would be missing important parts of the WebClient
features (like streaming). So the current advice still stands.
Comment From: nightswimmings
Ok, thanks! I thought that the fact WebTestClient supported now MockMVC enabled somehow the use of MockRestServiceServer for internal mocking