Interface MockMvcWebTestClient


public interface MockMvcWebTestClient
The main class for testing Spring MVC applications via WebTestClient with MockMvc for server request handling.

Provides static factory methods and specs to initialize MockMvc to which the WebTestClient connects to. For example:

 WebTestClient client = MockMvcWebTestClient.bindToController(myController)
         .controllerAdvice(myControllerAdvice)
         .validator(myValidator)
         .build()
 

The client itself can also be configured. For example:

 WebTestClient client = MockMvcWebTestClient.bindToController(myController)
         .validator(myValidator)
         .configureClient()
         .baseUrl("/path")
         .build();
 
Since:
5.3
Author:
Rossen Stoyanchev
  • Method Details

    • bindToController

      static MockMvcWebTestClient.ControllerSpec bindToController(Object... controllers)
      Begin creating a WebTestClient by providing the @Controller instance(s) to handle requests with.

      Internally this is delegated to and equivalent to using MockMvcBuilders.standaloneSetup(Object...). to initialize MockMvc.

    • bindToApplicationContext

      static MockMvcWebTestClient.MockMvcServerSpec<?> bindToApplicationContext(WebApplicationContext context)
      Begin creating a WebTestClient by providing a WebApplicationContext with Spring MVC infrastructure and controllers.

      Internally this is delegated to and equivalent to using MockMvcBuilders.webAppContextSetup(WebApplicationContext) to initialize MockMvc.

    • bindTo

      static WebTestClient.Builder bindTo(MockMvc mockMvc)
      Begin creating a WebTestClient by providing an already initialized MockMvc instance to use as the server.
    • resultActionsFor

      static ResultActions resultActionsFor(ExchangeResult exchangeResult)
      This method can be used to apply further assertions on a given ExchangeResult based the state of the server response.

      Normally WebTestClient is used to assert the client response including HTTP status, headers, and body. That is all that is available when making a live request over HTTP. However when the server is MockMvc, many more assertions are possible against the server response, e.g. model attributes, flash attributes, etc.

      Example:

       EntityExchangeResult<Void> result =
                      webTestClient.post().uri("/people/123")
                                      .exchange()
                                      .expectStatus().isFound()
                                      .expectHeader().location("/persons/Joe")
                                      .expectBody().isEmpty();
      
       MockMvcWebTestClient.resultActionsFor(result)
                      .andExpect(model().size(1))
                      .andExpect(model().attributeExists("name"))
                      .andExpect(flash().attributeCount(1))
                      .andExpect(flash().attribute("message", "success!"));
       

      Note: this method works only if the WebTestClient used to perform the request was initialized through one of bind method in this class, and therefore requests are handled by MockMvc.