Interface WebTestClient
public interface WebTestClient
Client for testing web servers that uses 
WebClient internally to
perform requests while also providing a fluent API to verify responses.
This client can connect to any server over HTTP, or to a WebFlux application
with a mock request and response.
Use one of the bindToXxx methods to create an instance. For example:
- Since:
- 5.0
- Author:
- Rossen Stoyanchev, Brian Clozel, Sam Brannen, MichaĆ Rowicki
- 
Nested Class SummaryNested ClassesModifier and TypeInterfaceDescriptionstatic interfaceSpec for expectations on the response body content.static interfaceWebTestClient.BodySpec<B, S extends WebTestClient.BodySpec<B,S>> Spec for expectations on the response body decoded to a single Object.static interfaceSteps to customize the underlyingWebClientviaWebClient.Builder.static interfaceSpecification for customizing controller configuration equivalent to, and internally delegating to, aWebFluxConfigurer.static interfaceWebTestClient.ListBodySpec<E extends @Nullable Object>Spec for expectations on the response body decoded to a List.static interfaceBase specification for setting up tests without a server.static interfaceSpecification for providing the body of a request.static interfaceSpecification for providing the body and the URI of a request.static interfaceSpecification for adding request headers and performing an exchange.static interfaceSpecification for providing request headers and the URI of a request.static interfaceChained API for applying assertions to a response.static interfaceSpecification for customizing router function configuration.static interfaceWebTestClient.UriSpec<S extends WebTestClient.RequestHeadersSpec<?>>Specification for providing the URI of a request.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThe name of a request header used to assign a unique id to every request performed through theWebTestClient.
- 
Method SummaryModifier and TypeMethodDescriptionstatic WebTestClient.MockServerSpec<?> bindToApplicationContext(ApplicationContext applicationContext) Use this option to set up a server from the Spring configuration of your application, or some subset of it.static WebTestClient.ControllerSpecbindToController(Object... controllers) Begin creating aWebTestClientwith a mock server setup that tests one@Controllerat a time with@EnableWebFluxequivalent configuration.bindToRouterFunction(RouterFunction<?> routerFunction) Begin creating aWebTestClientwith a mock server setup that tests oneRouterFunctionat a time with@EnableWebFluxequivalent configuration.static WebTestClient.BuilderThis server setup option allows you to connect to a live server.static WebTestClient.BuilderbindToServer(ClientHttpConnector connector) A variant ofbindToServer()with a pre-configured connector.static WebTestClient.MockServerSpec<?> bindToWebHandler(WebHandler webHandler) Integration testing with a "mock" server targeting the given WebHandler.delete()Prepare an HTTP DELETE request.get()Prepare an HTTP GET request.head()Prepare an HTTP HEAD request.method(HttpMethod method) Prepare a request for the specifiedHttpMethod.mutate()Return a builder to mutate properties of this test client.mutateWith(WebTestClientConfigurer configurer) Mutate theWebTestClient, apply the given configurer, and build a new instance.options()Prepare an HTTP OPTIONS request.patch()Prepare an HTTP PATCH request.post()Prepare an HTTP POST request.put()Prepare an HTTP PUT request.
- 
Field Details- 
WEBTESTCLIENT_REQUEST_IDThe name of a request header used to assign a unique id to every request performed through theWebTestClient. This can be useful for storing contextual information at all phases of request processing (for example, from a server-side component) under that id and later to look up that information once anExchangeResultis available.- See Also:
 
 
- 
- 
Method Details- 
getWebTestClient.RequestHeadersUriSpec<?> get()Prepare an HTTP GET request.- Returns:
- a spec for specifying the target URL
 
- 
headWebTestClient.RequestHeadersUriSpec<?> head()Prepare an HTTP HEAD request.- Returns:
- a spec for specifying the target URL
 
- 
postPrepare an HTTP POST request.- Returns:
- a spec for specifying the target URL
 
- 
putPrepare an HTTP PUT request.- Returns:
- a spec for specifying the target URL
 
- 
patchWebTestClient.RequestBodyUriSpec patch()Prepare an HTTP PATCH request.- Returns:
- a spec for specifying the target URL
 
- 
deleteWebTestClient.RequestHeadersUriSpec<?> delete()Prepare an HTTP DELETE request.- Returns:
- a spec for specifying the target URL
 
- 
optionsWebTestClient.RequestHeadersUriSpec<?> options()Prepare an HTTP OPTIONS request.- Returns:
- a spec for specifying the target URL
 
- 
methodPrepare a request for the specifiedHttpMethod.- Returns:
- a spec for specifying the target URL
 
- 
mutateWebTestClient.Builder mutate()Return a builder to mutate properties of this test client.
- 
mutateWithMutate theWebTestClient, apply the given configurer, and build a new instance. Essentially a shortcut for:mutate().apply(configurer).build(); - Parameters:
- configurer- the configurer to apply
- Returns:
- the mutated test client
 
- 
bindToControllerBegin creating aWebTestClientwith a mock server setup that tests one@Controllerat a time with@EnableWebFluxequivalent configuration.- Parameters:
- controllers- one or more controller instances to test (specified- Classwill be turned into instance)
- Returns:
- chained API to customize server and client config; use
WebTestClient.MockServerSpec.configureClient()to transition to client config
 
- 
bindToRouterFunctionBegin creating aWebTestClientwith a mock server setup that tests oneRouterFunctionat a time with@EnableWebFluxequivalent configuration.- Parameters:
- routerFunction- the RouterFunction to test
- Returns:
- chained API to customize server and client config; use
WebTestClient.MockServerSpec.configureClient()to transition to client config
 
- 
bindToApplicationContextstatic WebTestClient.MockServerSpec<?> bindToApplicationContext(ApplicationContext applicationContext) Use this option to set up a server from the Spring configuration of your application, or some subset of it. Internally the provided configuration is passed toWebHttpHandlerBuilderto set up the request processing chain. The resulting WebFlux application will be tested without an HTTP server using a mock request and response.Consider using the TestContext framework and @ContextConfigurationin order to efficiently load and inject the Spring configuration into the test class.- Parameters:
- applicationContext- the Spring context
- Returns:
- chained API to customize server and client config; use
WebTestClient.MockServerSpec.configureClient()to transition to client config
 
- 
bindToWebHandlerIntegration testing with a "mock" server targeting the given WebHandler.- Parameters:
- webHandler- the handler to test
- Returns:
- chained API to customize server and client config; use
WebTestClient.MockServerSpec.configureClient()to transition to client config
 
- 
bindToServerThis server setup option allows you to connect to a live server.WebTestClient client = WebTestClient.bindToServer() .baseUrl("http://localhost:8080") .build();- Returns:
- chained API to customize client config
 
- 
bindToServerA variant ofbindToServer()with a pre-configured connector.- Returns:
- chained API to customize client config
- Since:
- 5.0.2
 
 
-