public interface MockMvcWebTestClient
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();
Modifier and Type | Interface and Description |
---|---|
static interface |
MockMvcWebTestClient.ControllerSpec
Specification for configuring
MockMvc to test one or more
controllers directly, and a simple facade around
StandaloneMockMvcBuilder . |
static interface |
MockMvcWebTestClient.MockMvcServerSpec<B extends MockMvcWebTestClient.MockMvcServerSpec<B>>
Base specification for configuring
MockMvc , and a simple facade
around ConfigurableMockMvcBuilder . |
Modifier and Type | Method and Description |
---|---|
static WebTestClient.Builder |
bindTo(MockMvc mockMvc)
Begin creating a
WebTestClient by providing an already
initialized MockMvc instance to use as the server. |
static MockMvcWebTestClient.MockMvcServerSpec<?> |
bindToApplicationContext(WebApplicationContext context)
Begin creating a
WebTestClient by providing a
WebApplicationContext with Spring MVC infrastructure and
controllers. |
static MockMvcWebTestClient.ControllerSpec |
bindToController(Object... controllers)
Begin creating a
WebTestClient by providing the @Controller
instance(s) to handle requests with. |
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. |
static MockMvcWebTestClient.ControllerSpec bindToController(Object... controllers)
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
.
static MockMvcWebTestClient.MockMvcServerSpec<?> bindToApplicationContext(WebApplicationContext context)
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
.
static WebTestClient.Builder bindTo(MockMvc mockMvc)
WebTestClient
by providing an already
initialized MockMvc
instance to use as the server.static ResultActions resultActionsFor(ExchangeResult exchangeResult)
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
.