Package org.springframework.test.web.reactive.server

# 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:

- `bindToController(Object...)`
- `bindToRouterFunction(RouterFunction)`
- `bindToApplicationContext(ApplicationContext)`
- `bindToServer()`
- ...

Since:
  5.0
Author:
  Rossen Stoyanchev, Brian Clozel, Sam Brannen, Michał Rowicki
See Also:
  - `StatusAssertions`
  - `HeaderAssertions`
  - `JsonPathAssertions`

## Nested Class Summary

Nested Classes
Modifier and Type
Interface
Description
`static interface`
`WebTestClient.BodyContentSpec`
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`
`WebTestClient.Builder`
Steps for customizing the `WebClient` used to test with, internally delegating to a `WebClient.Builder`.
`static interface`
`WebTestClient.ControllerSpec`
Specification for customizing controller configuration equivalent to, and internally delegating to, a `WebFluxConfigurer`.
`static interface`
`WebTestClient.ListBodySpec<E>`
Spec for expectations on the response body decoded to a List.
`static interface`
`WebTestClient.MockServerSpec<B extends WebTestClient.MockServerSpec<B>>`
Base specification for setting up tests without a server.
`static interface`
`WebTestClient.RequestBodySpec`
Specification for providing body of a request.
`static interface`
`WebTestClient.RequestBodyUriSpec`
Specification for providing the body and the URI of a request.
`static interface`
`WebTestClient.RequestHeadersSpec<S extends WebTestClient.RequestHeadersSpec<S>>`
Specification for adding request headers and performing an exchange.
`static interface`
`WebTestClient.RequestHeadersUriSpec<S extends WebTestClient.RequestHeadersSpec<S>>`
Specification for providing request headers and the URI of a request.
`static interface`
`WebTestClient.ResponseSpec`
Chained API for applying assertions to a response.
`static interface`
`WebTestClient.RouterFunctionSpec`
Specification for customizing router function configuration.
`static interface`
`WebTestClient.UriSpec<S extends WebTestClient.RequestHeadersSpec<?>>`
Specification for providing the URI of a request.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`WEBTESTCLIENT_REQUEST_ID`
The name of a request header used to assign a unique id to every request performed through the `WebTestClient`.

## Method Summary

Modifier and Type
Method
Description
`static WebTestClient.MockServerSpec<?>`
`bindToApplicationContext(org.springframework.context.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.
`static WebTestClient.RouterFunctionSpec`
`bindToRouterFunction(org.springframework.web.reactive.function.server.RouterFunction<?> routerFunction)`
Use this option to set up a server from a `RouterFunction`.
`static WebTestClient.Builder`
`bindToServer()`
This server setup option allows you to connect to a live server through a Reactor Netty client connector.
`static WebTestClient.Builder`
`bindToServer(org.springframework.http.client.reactive.ClientHttpConnector connector)`
A variant of `bindToServer()` with a pre-configured connector.
`static WebTestClient.MockServerSpec<?>`
`bindToWebHandler(org.springframework.web.server.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.RequestBodyUriSpec`
`method(org.springframework.http.HttpMethod method)`
Prepare a request for the specified `HttpMethod`.
`WebTestClient.Builder`
`mutate()`
Return a builder to mutate properties of this web test client.
`WebTestClient`
`mutateWith(WebTestClientConfigurer configurer)`
Mutate the `WebTestClient`, apply the given configurer, and build a new instance.
`WebTestClient.RequestHeadersUriSpec<?>`
`options()`
Prepare an HTTP OPTIONS request.
`WebTestClient.RequestBodyUriSpec`
`patch()`
Prepare an HTTP PATCH request.
`WebTestClient.RequestBodyUriSpec`
`post()`
Prepare an HTTP POST request.
`WebTestClient.RequestBodyUriSpec`
`put()`
Prepare an HTTP PUT request.

## Field Details

### 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 the `WebTestClient`. 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 an `ExchangeResult` is available.

See Also:
  - Constant Field Values

## 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

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(org.springframework.http.HttpMethod method)
Prepare a request for the specified `HttpMethod`.

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 the `WebTestClient`, 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 test (specified `Class` will 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(org.springframework.web.reactive.function.server.RouterFunction<?> routerFunction)
Use this option to set up a server from a `RouterFunction`. Internally the provided configuration is passed to `RouterFunctions#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(org.springframework.context.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 to `WebHttpHandlerBuilder` 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

static WebTestClient.MockServerSpec<?> bindToWebHandler(org.springframework.web.server.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 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

static WebTestClient.Builder bindToServer(org.springframework.http.client.reactive.ClientHttpConnector connector)
A variant of `bindToServer()` with a pre-configured connector.

Returns:
  chained API to customize client config
Since:
  5.0.2