28

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

devsh
  • 331
  • 1
  • 4
  • 10
  • 2
    From the documentation, WebTestClient is used to test WebFlux application while MockMVC is used to normal MVC web application – cdxf Apr 02 '18 at 21:26

1 Answers1

25

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

ch271828n
  • 15,854
  • 5
  • 53
  • 88
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • It seems like there is some ability to use both `spring-boot-starter-web` and `spring-boot-starter-webflux` in the same app, but trying to do so may be more trouble than it's worth. Basically, these are two separate and mutually-exclusive stacks. – Brent Bradburn Apr 22 '18 at 18:18
  • 2
    As far as I know, using mvc and webflux in the same application is not supported. – Jan Mares May 14 '19 at 07:02
  • 1
    and also I know ,also you can use webtestclient with tomcat – Mithat Konuk Aug 23 '19 at 09:54
  • 5
    `spring-boot-starter-web` is compatible with `WebClient` and `WebTestClient`. In fact, I believe it's recommended now since the use of `RestTemplate` is currently discouraged. – Jefferson Lima Oct 05 '21 at 07:31