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
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceSpecification for configuringMockMvcto test one or more controllers directly, and a simple facade aroundStandaloneMockMvcBuilder.static interfaceBase specification for configuringMockMvc, and a simple facade aroundConfigurableMockMvcBuilder.static interfaceSpecification for configuringMockMvcto test one or more router functions directly, and a simple facade aroundRouterFunctionMockMvcBuilder. -
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic WebTestClient.BuilderBegin creating aWebTestClientby providing an already initializedMockMvcinstance to use as the server.static MockMvcWebTestClient.MockMvcServerSpec<?> Begin creating aWebTestClientby providing aWebApplicationContextwith Spring MVC infrastructure and controllers.bindToController(Object... controllers) Begin creating aWebTestClientby providing the@Controllerinstance(s) to handle requests with.bindToRouterFunction(RouterFunction<?>... routerFunctions) Begin creating aWebTestClientby providing theRouterFunctioninstance(s) to handle requests with.static ResultActionsresultActionsFor(ExchangeResult exchangeResult) This method can be used to apply further assertions on a givenExchangeResultbased the state of the server response.
-
Method Details
-
bindToController
Begin creating aWebTestClientby providing the@Controllerinstance(s) to handle requests with.Internally this is delegated to and equivalent to using
MockMvcBuilders.standaloneSetup(Object...)to initializeMockMvc. -
bindToRouterFunction
static MockMvcWebTestClient.RouterFunctionSpec bindToRouterFunction(RouterFunction<?>... routerFunctions) Begin creating aWebTestClientby providing theRouterFunctioninstance(s) to handle requests with.Internally this is delegated to and equivalent to using
MockMvcBuilders.routerFunctions(RouterFunction[])to initializeMockMvc.- Since:
- 6.2
-
bindToApplicationContext
static MockMvcWebTestClient.MockMvcServerSpec<?> bindToApplicationContext(WebApplicationContext context) Begin creating aWebTestClientby providing aWebApplicationContextwith Spring MVC infrastructure and controllers.Internally this is delegated to and equivalent to using
MockMvcBuilders.webAppContextSetup(WebApplicationContext)to initializeMockMvc. -
bindTo
Begin creating aWebTestClientby providing an already initializedMockMvcinstance to use as the server. -
resultActionsFor
This method can be used to apply further assertions on a givenExchangeResultbased the state of the server response.Normally
WebTestClientis 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, for example, 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
WebTestClientused to perform the request was initialized through one of bind method in this class, and therefore requests are handled byMockMvc.
-