接口 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();
- 从以下版本开始:
- 4.0
- 作者:
- Rossen Stoyanchev
-
嵌套类概要
嵌套类修饰符和类型接口说明static 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 WebTestClient.BuilderBegin creating aWebTestClientby providing an already initializedMockMvcinstance to use as the server.static MockMvcWebTestClient.MockMvcServerSpec<?>bindToApplicationContext(cn.taketoday.web.servlet.WebApplicationContext context) Begin creating aWebTestClientby providing aWebApplicationContextwith Web MVC infrastructure and controllers.bindToController(Object... controllers) Begin creating aWebTestClientby providing the@Controllerinstance(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.
-
方法详细资料
-
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. -
bindToApplicationContext
static MockMvcWebTestClient.MockMvcServerSpec<?> bindToApplicationContext(cn.taketoday.web.servlet.WebApplicationContext context) Begin creating aWebTestClientby providing aWebApplicationContextwith Web 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, 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
WebTestClientused to perform the request was initialized through one of bind method in this class, and therefore requests are handled byMockMvc.
-