Interface WebTestClient
public interface WebTestClient
Client for testing web servers that usesWebClientinternally 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:
bindToController(Object...)bindToRouterFunction(RouterFunction)bindToApplicationContext(ApplicationContext)bindToServer()- ...
Warning:
WebTestClientis not usable yet in Kotlin due to a type inference issue which is expected to be fixed as of Kotlin 1.3. You can watch gh-20606 for up-to-date information. Meanwhile, the proposed alternative is to use directlyWebClientwith its Reactor and Spring Kotlin extensions to perform integration tests on an embedded WebFlux server.- Since:
- 5.0
- Author:
- Rossen Stoyanchev, Brian Clozel
- See Also:
StatusAssertions,HeaderAssertions,JsonPathAssertions
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceWebTestClient.BodyContentSpecSpec 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 interfaceWebTestClient.BuilderSteps for customizing theWebClientused to test with, internally delegating to aWebClient.Builder.static interfaceWebTestClient.ControllerSpecSpecification for customizing controller configuration equivalent to, and internally delegating to, aWebFluxConfigurer.static interfaceWebTestClient.ListBodySpec<E>Spec for expectations on the response body decoded to a List.static interfaceWebTestClient.MockServerSpec<B extends WebTestClient.MockServerSpec<B>>Base specification for setting up tests without a server.static interfaceWebTestClient.RequestBodySpecSpecification for providing body of a request.static interfaceWebTestClient.RequestBodyUriSpecSpecification for providing the body and the URI of a request.static interfaceWebTestClient.RequestHeadersSpec<S extends WebTestClient.RequestHeadersSpec<S>>Specification for adding request headers and performing an exchange.static interfaceWebTestClient.RequestHeadersUriSpec<S extends WebTestClient.RequestHeadersSpec<S>>Specification for providing request headers and the URI of a request.static interfaceWebTestClient.ResponseSpecChained API for applying assertions to a response.static interfaceWebTestClient.RouterFunctionSpecSpecification for customizing router function configuration.static interfaceWebTestClient.UriSpec<S extends WebTestClient.RequestHeadersSpec<?>>Specification for providing the URI of a request.
Field Summary
Fields Modifier and Type Field Description static StringWEBTESTCLIENT_REQUEST_IDThe name of a request header used to assign a unique id to every request performed through theWebTestClient.
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description static WebTestClient.MockServerSpec<?>bindToApplicationContext(ApplicationContext applicationContext)Use this option to setup a server from the Spring configuration of your application, or some subset of it.static WebTestClient.ControllerSpecbindToController(Object... controllers)Use this server setup to test one `@Controller` at a time.static WebTestClient.RouterFunctionSpecbindToRouterFunction(RouterFunction<?> routerFunction)Use this option to set up a server from aRouterFunction.static WebTestClient.BuilderbindToServer()This server setup option allows you to connect to a running server via Reactor Netty.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.WebTestClient.RequestHeadersUriSpec<?>delete()Prepare an HTTP DELETE request.WebTestClient.RequestHeadersUriSpec<?>get()Prepare an HTTP GET request.WebTestClient.RequestHeadersUriSpec<?>head()Prepare an HTTP HEAD request.WebTestClient.RequestBodyUriSpecmethod(HttpMethod method)Prepare a request for the specifiedHttpMethod.WebTestClient.Buildermutate()Return a builder to mutate properties of this web test client.WebTestClientmutateWith(WebTestClientConfigurer configurer)Mutate theWebTestClient, apply the given configurer, and build a new instance.WebTestClient.RequestHeadersUriSpec<?>options()Prepare an HTTP OPTIONS request.WebTestClient.RequestBodyUriSpecpatch()Prepare an HTTP PATCH request.WebTestClient.RequestBodyUriSpecpost()Prepare an HTTP POST request.WebTestClient.RequestBodyUriSpecput()Prepare an HTTP PUT request.
Field Detail
WEBTESTCLIENT_REQUEST_ID
static final String 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 anExchangeResultis available.- See Also:
- Constant Field Values
Method Detail
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
WebTestClient.RequestBodyUriSpec post()
Prepare an HTTP POST request.- Returns:
- a spec for specifying the target URL
put
WebTestClient.RequestBodyUriSpec 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
WebTestClient.RequestBodyUriSpec method(HttpMethod 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
WebTestClient mutateWith(WebTestClientConfigurer configurer)
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
static WebTestClient.ControllerSpec bindToController(Object... controllers)
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 tests (specifiedClasswill be turned into instance)- Returns:
- chained API to customize server and client config; use
WebTestClient.MockServerSpec.configureClient()to transition to client config
bindToRouterFunction
static WebTestClient.RouterFunctionSpec bindToRouterFunction(RouterFunction<?> routerFunction)
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 setup 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
bindToWebHandler
static WebTestClient.MockServerSpec<?> bindToWebHandler(WebHandler webHandler)
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
static WebTestClient.Builder bindToServer()
This server setup option allows you to connect to a running server via Reactor Netty.WebTestClient client = WebTestClient.bindToServer() .baseUrl("http://localhost:8080") .build();- Returns:
- chained API to customize client config
bindToServer
static WebTestClient.Builder bindToServer(ClientHttpConnector connector)
A variant ofbindToServer()with a pre-configured connector.WebTestClient client = WebTestClient.bindToServer() .baseUrl("http://localhost:8080") .build();- Returns:
- chained API to customize client config
- Since:
- 5.0.2