Skip to content

Configure WebTestClient for @AutoConfigureMockMvc tests #23067

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

Closed
bclozel opened this issue Aug 24, 2020 · 4 comments
Closed

Configure WebTestClient for @AutoConfigureMockMvc tests #23067

bclozel opened this issue Aug 24, 2020 · 4 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@bclozel
Copy link
Member

bclozel commented Aug 24, 2020

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.

@bclozel bclozel added type: enhancement A general enhancement status: pending-design-work Needs design work before any code can be developed labels Aug 24, 2020
@bclozel bclozel added this to the 2.4.x milestone Aug 24, 2020
@philwebb philwebb modified the milestones: 2.4.x, 2.5.x Oct 21, 2020
@Sam-Kruglov
Copy link

Sam-Kruglov commented Dec 6, 2020

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

@philwebb philwebb modified the milestones: 2.5.x, 2.6.x Mar 19, 2021
@philwebb philwebb removed the status: pending-design-work Needs design work before any code can be developed label Jun 14, 2021
@bclozel bclozel changed the title Investigate WebTestClient support for MockMvc in testing infrastructure Configure WebTestClient for @AutoConfigureMockMvc tests Aug 3, 2021
@bclozel bclozel modified the milestones: 2.6.x, 2.6.0-M2 Aug 3, 2021
@bclozel bclozel closed this as completed in 8b3bea1 Aug 3, 2021
@nightswimmings
Copy link

@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? spring-projects/spring-framework#19852

@bclozel
Copy link
Member Author

bclozel commented Jan 29, 2022

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.

@nightswimmings
Copy link

Ok, thanks! I thought that the fact WebTestClient supported now MockMVC enabled somehow the use of MockRestServiceServer for internal mocking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants