问题
When I tried to test at Spring 4.x, I used MockMvc web client, but I am reading and trying new features of Spring 5.x.
I think, WebTestClient and MockMvc are same or very similar.
What is the difference between MockMvc and WebTestClient ?
I am waiting for your answer. Thank you
回答1:
Similarities
- Both provide a fluent-style syntax for testing web services.
- Both can or do operate in a simulated environment that bypasses the use of HTTP.
Major Differences
- WebTestClient can also be used to test real web services using HTTP.
- Specify
@SpringBootTest
instead of@WebFluxText
.
- Specify
- WebTestClient only works if you are using Netty for your local server.
- This feels like an artificial limitation for the test environment.
- It is likely due to the non-blocking nature of the underlying WebClient.
- WebTestClient can test Streaming Responses
Resources
- Benefits of having HTTP endpoints return Flux/Mono instances instead of DTOs
- Unable to use reactive WebClient without spring-boot-starter-reactor-netty
- WebTestClient documentation
- Spring Boot Data Rest don't support reactive? -- limitations of Netty
来源:https://stackoverflow.com/questions/49330878/what-is-the-difference-between-mockmvc-and-webtestclient