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
via mock request and response objects.
Use one of the bindToXxx methods to create an instance. For example:
- Since:
- 5.0
- Author:
- Rossen Stoyanchev, Brian Clozel, Sam Brannen, MichaĆ Rowicki
- See Also:
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Spec for expectations on the response body content.static interface
WebTestClient.BodySpec<B,
S extends WebTestClient.BodySpec<B, S>> Spec for expectations on the response body decoded to a single Object.static interface
Steps for customizing theWebClient
used to test with, internally delegating to aWebClient.Builder
.static interface
Specification for customizing controller configuration equivalent to, and internally delegating to, aWebFluxConfigurer
.static interface
Spec for expectations on the response body decoded to a List.static interface
Base specification for setting up tests without a server.static interface
Specification for providing body of a request.static interface
Specification for providing the body and the URI of a request.static interface
Specification for adding request headers and performing an exchange.static interface
Specification for providing request headers and the URI of a request.static interface
Chained API for applying assertions to a response.static interface
Specification for customizing router function configuration.static interface
WebTestClient.UriSpec<S extends WebTestClient.RequestHeadersSpec<?>>
Specification for providing the URI of a request. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The name of a request header used to assign a unique id to every request performed through theWebTestClient
. -
Method Summary
Modifier 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.ControllerSpec
bindToController
(Object... controllers) Use this server setup to test one@Controller
at a time.bindToRouterFunction
(RouterFunction<?> routerFunction) Use this option to set up a server from aRouterFunction
.static WebTestClient.Builder
This server setup option allows you to connect to a live server through a Reactor Netty client connector.static WebTestClient.Builder
bindToServer
(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 web 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_ID
The 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 (e.g. from a server-side component) under that id and later to look up that information once anExchangeResult
is available.- See Also:
-
-
Method Details
-
get
WebTestClient.RequestHeadersUriSpec<?> get()Prepare an HTTP GET request.- Returns:
- a spec for specifying the target URL
-
head
WebTestClient.RequestHeadersUriSpec<?> head()Prepare an HTTP HEAD request.- Returns:
- a spec for specifying the target URL
-
post
Prepare an HTTP POST request.- Returns:
- a spec for specifying the target URL
-
put
Prepare an HTTP PUT request.- Returns:
- a spec for specifying the target URL
-
patch
WebTestClient.RequestBodyUriSpec patch()Prepare an HTTP PATCH request.- Returns:
- a spec for specifying the target URL
-
delete
WebTestClient.RequestHeadersUriSpec<?> delete()Prepare an HTTP DELETE request.- Returns:
- a spec for specifying the target URL
-
options
WebTestClient.RequestHeadersUriSpec<?> options()Prepare an HTTP OPTIONS request.- Returns:
- a spec for specifying the target URL
-
method
Prepare a request for the specifiedHttpMethod
.- Returns:
- a spec for specifying the target URL
-
mutate
WebTestClient.Builder mutate()Return a builder to mutate properties of this web test client. -
mutateWith
Mutate 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
-
bindToController
Use this server setup to test one@Controller
at a time. This option loads the default configuration of@EnableWebFlux
. There are builder methods to customize the Java config. The resulting WebFlux application will be tested without an HTTP server using a mock request and response.- Parameters:
controllers
- one or more controller instances to test (specifiedClass
will be turned into instance)- Returns:
- chained API to customize server and client config; use
WebTestClient.MockServerSpec.configureClient()
to transition to client config
-
bindToRouterFunction
Use this option to set up a server from aRouterFunction
. Internally the provided configuration is passed toRouterFunctions#toWebHandler
. The resulting WebFlux application will be tested without an HTTP server using a mock request and response.- Parameters:
routerFunction
- the RouterFunction to test- Returns:
- chained API to customize server and client config; use
WebTestClient.MockServerSpec.configureClient()
to transition to client config
-
bindToApplicationContext
static 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 toWebHttpHandlerBuilder
to 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
@ContextConfiguration
in 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
-
bindToWebHandler
Integration 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
-
bindToServer
This server setup option allows you to connect to a live server through a Reactor Netty client connector.WebTestClient client = WebTestClient.bindToServer() .baseUrl("http://localhost:8080") .build();
- Returns:
- chained API to customize client config
-
bindToServer
A variant ofbindToServer()
with a pre-configured connector.- Returns:
- chained API to customize client config
- Since:
- 5.0.2
-