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();
- Since:
- 5.3
- Author:
- Rossen Stoyanchev
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Specification for configuringMockMvc
to test one or more controllers directly, and a simple facade aroundStandaloneMockMvcBuilder
.static interface
Base specification for configuringMockMvc
, and a simple facade aroundConfigurableMockMvcBuilder
. -
Method Summary
Modifier and TypeMethodDescriptionstatic WebTestClient.Builder
Begin creating aWebTestClient
by providing an already initializedMockMvc
instance to use as the server.static MockMvcWebTestClient.MockMvcServerSpec<?>
Begin creating aWebTestClient
by providing aWebApplicationContext
with Spring MVC infrastructure and controllers.bindToController
(Object... controllers) Begin creating aWebTestClient
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 givenExchangeResult
based the state of the server response.
-
Method Details
-
bindToController
Begin creating aWebTestClient
by providing the@Controller
instance(s) to handle requests with.Internally this is delegated to and equivalent to using
MockMvcBuilders.standaloneSetup(Object...)
. to initializeMockMvc
. -
bindToApplicationContext
static MockMvcWebTestClient.MockMvcServerSpec<?> bindToApplicationContext(WebApplicationContext context) Begin creating aWebTestClient
by providing aWebApplicationContext
with Spring MVC infrastructure and controllers.Internally this is delegated to and equivalent to using
MockMvcBuilders.webAppContextSetup(WebApplicationContext)
to initializeMockMvc
. -
bindTo
Begin creating aWebTestClient
by providing an already initializedMockMvc
instance to use as the server. -
resultActionsFor
This method can be used to apply further assertions on a givenExchangeResult
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 isMockMvc
, 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 byMockMvc
.
-