Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between MockMvc and WebTestClient?

Tags:

spring

mockmvc

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

like image 587
devsh Avatar asked Mar 16 '18 23:03

devsh


People also ask

What is WebTestClient?

WebTestClient is a thin shell around WebClient, using it to perform requests and exposing a dedicated, fluent API for verifying responses. WebTestClient binds to a WebFlux application by using a mock request and response, or it can test any web server over an HTTP connection.

What is a MockMvc?

MockMvc is defined as a main entry point for server-side Spring MVC testing. Tests with MockMvc lie somewhere between between unit and integration tests.

What is MockMvc content?

MockMVC class is part of Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container. In this MockMVC tutorial, we will use it along with Spring boot's WebMvcTest class to execute Junit testcases which tests REST controller methods written for Spring boot 2 hateoas example.

Is MockMvc thread safe?

From a technical point of view MockMvc is not thread-safe and shouldn't be reused. These setters are package private and a MockMvc instance can be acquired only through MockMvcBuilders . Hence you can't manipulte a MockMvc instance afterwards so that it is actually resuable across multiple tests.


1 Answers

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 @WebFluxTest.
  • 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
like image 104
Brent Bradburn Avatar answered Oct 05 '22 06:10

Brent Bradburn