接口 WebTestClient
public interface WebTestClient
Client for testing web servers that usesWebClient
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()
- ...
Warning:
WebTestClient
is 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 directlyWebClient
with its Reactor and Spring Kotlin extensions to perform integration tests on an embedded WebFlux server.- 从以下版本开始:
- 5.0
- 作者:
- Rossen Stoyanchev, Brian Clozel
- 另请参阅:
StatusAssertions
,HeaderAssertions
,JsonPathAssertions
嵌套类概要
嵌套类 修饰符和类型 接口 说明 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 theWebClient
used to test with, internally delegating to aWebClient.Builder
.static interface
WebTestClient.ControllerSpec
Specification for customizing controller configuration equivalent to, and internally delegating to, aWebFluxConfigurer
.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.
字段概要
字段 修饰符和类型 字段 说明 static String
WEBTESTCLIENT_REQUEST_ID
The name of a request header used to assign a unique id to every request performed through theWebTestClient
.
方法概要
所有方法 静态方法 实例方法 抽象方法 修饰符和类型 方法 说明 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.ControllerSpec
bindToController(Object... controllers)
Use this server setup to test one `@Controller` at a time.static WebTestClient.RouterFunctionSpec
bindToRouterFunction(RouterFunction<?> routerFunction)
Use this option to set up a server from aRouterFunction
.static WebTestClient.Builder
bindToServer()
This server setup option allows you to connect to a running server via Reactor Netty.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.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(HttpMethod method)
Prepare a request for the specifiedHttpMethod
.WebTestClient.Builder
mutate()
Return a builder to mutate properties of this web test client.WebTestClient
mutateWith(WebTestClientConfigurer configurer)
Mutate theWebTestClient
, 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.
字段详细资料
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 anExchangeResult
is available.- 另请参阅:
- 常量字段值
方法详细资料
get
WebTestClient.RequestHeadersUriSpec<?> get()
Prepare an HTTP GET request.- 返回:
- a spec for specifying the target URL
head
WebTestClient.RequestHeadersUriSpec<?> head()
Prepare an HTTP HEAD request.- 返回:
- a spec for specifying the target URL
post
WebTestClient.RequestBodyUriSpec post()
Prepare an HTTP POST request.- 返回:
- a spec for specifying the target URL
put
WebTestClient.RequestBodyUriSpec put()
Prepare an HTTP PUT request.- 返回:
- a spec for specifying the target URL
patch
WebTestClient.RequestBodyUriSpec patch()
Prepare an HTTP PATCH request.- 返回:
- a spec for specifying the target URL
delete
WebTestClient.RequestHeadersUriSpec<?> delete()
Prepare an HTTP DELETE request.- 返回:
- a spec for specifying the target URL
options
WebTestClient.RequestHeadersUriSpec<?> options()
Prepare an HTTP OPTIONS request.- 返回:
- a spec for specifying the target URL
method
WebTestClient.RequestBodyUriSpec method(HttpMethod method)
Prepare a request for the specifiedHttpMethod
.- 返回:
- 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();
- 参数:
configurer
- the configurer to apply- 返回:
- 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.- 参数:
controllers
- one or more controller instances to tests (specifiedClass
will be turned into instance)- 返回:
- 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.- 参数:
routerFunction
- the RouterFunction to test- 返回:
- 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 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.- 参数:
applicationContext
- the Spring context- 返回:
- 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.- 参数:
webHandler
- the handler to test- 返回:
- 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();
- 返回:
- 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();
- 返回:
- chained API to customize client config
- 从以下版本开始:
- 5.0.2