Interface RestTestClient
public interface RestTestClient
Client for testing web servers that uses
RestClient 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 MockMvc
server with a mock request and response.
Use one of the bindToXxx() methods to create an instance. For example:
- since:
- 7.0
- Author:
- Rob Worsnop, Rossen stoyanchev
-
Nested Class summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface spec for expectations on the response body content.static interface RestTestClient.Bodyspec<B, s extends RestTestClient.Bodyspec<B,s>> spec for expectations on the response body decoded to a single Object.static interface RestTestClient.Builder<B extends RestTestClient.Builder<B>>steps to customize the underlyingRestClientviaRestClient.Builder.static interface RestTestClient.MockMvcsetupBuilder<s extends RestTestClient.Builder<s>, M extends MockMvcBuilder>Extension ofRestTestClient.Builderfor tests against a MockMvc server.static interface specification for providing the 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 Extension ofRestTestClient.Builderfor tests against а RouterFunction MockMvc setup.static interface Extension ofRestTestClient.Builderfor tests against а standalone MockMvc setup.static interface RestTestClient.Urispec<s extends RestTestClient.RequestHeadersspec<?>>specification for providing the URI of a request.static interface Extension ofRestTestClient.Builderfor tests against а WebAppContext MockMvc setup. -
Field summary
FieldsModifier and TypeFieldDescriptionstatic final stringThe name of a request header used to assign a unique ID to every request performed through theRestTestClient. -
Method summary
Modifier and TypeMethodDescriptionstatic RestTestClient.Builder<?> Begin creating aRestTestClientby providing an already initializedMockMvcinstance to use as the server.bindToApplicationContext(WebApplicationContext context) Begin creating aRestTestClientwith a WebApplicationContext MockMvc setup.bindToController(Object... controllers) Begin creating aRestTestClientwith a standalone MockMvc setup.bindToRouterFunction(RouterFunction<?>... routerFunctions) Begin creating aRestTestClientwith a RouterFunction MockMvc setup.static RestTestClient.Builder<?> This server setup option allows you to connect to a live server.static RestTestClient.Builder<?> bindToserver(ClientHttpRequestFactory requestFactory) A variant ofbindToserver()with a pre-configured request factory.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.<B extends RestTestClient.Builder<B>>
RestTestClient.Builder<B> mutate()Return a builder to mutate properties of this test client.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
-
REsTTEsTCLIENT_REQUEsT_ID
The name of a request header used to assign a unique ID to every request performed through theRestTestClient. This can be useful to store contextual information under that ID at all phases of request processing (for example, from a server-side component) and later look up that information once anExchangeResultis available.- see Also:
-
-
Method Details
-
get
RestTestClient.RequestHeadersUrispec<?> get()Prepare an HTTP GET request.- Returns:
- a spec for specifying the target URL
-
head
RestTestClient.RequestHeadersUrispec<?> head()Prepare an HTTP HEAD request.- Returns:
- a spec for specifying the target URL
-
post
RestTestClient.RequestBodyUrispec post()Prepare an HTTP POsT request.- Returns:
- a spec for specifying the target URL
-
put
RestTestClient.RequestBodyUrispec put()Prepare an HTTP PUT request.- Returns:
- a spec for specifying the target URL
-
patch
RestTestClient.RequestBodyUrispec patch()Prepare an HTTP PATCH request.- Returns:
- a spec for specifying the target URL
-
delete
RestTestClient.RequestHeadersUrispec<?> delete()Prepare an HTTP DELETE request.- Returns:
- a spec for specifying the target URL
-
options
RestTestClient.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
Return a builder to mutate properties of this test client. -
bindToController
Begin creating aRestTestClientwith a standalone MockMvc setup. -
bindToRouterFunction
static RestTestClient.RouterFunctionsetupBuilder bindToRouterFunction(RouterFunction<?>... routerFunctions) Begin creating aRestTestClientwith a RouterFunction MockMvc setup. -
bindToApplicationContext
static RestTestClient.WebAppContextsetupBuilder bindToApplicationContext(WebApplicationContext context) Begin creating aRestTestClientwith a WebApplicationContext MockMvc setup. -
bindTo
Begin creating aRestTestClientby providing an already initializedMockMvcinstance to use as the server. -
bindToserver
This server setup option allows you to connect to a live server.RestTestClient client = RestTestClient.bindToserver() .baseUrl("http://localhost:8080") .build();- Returns:
- chained API to customize client config
-
bindToserver
static RestTestClient.Builder<?> bindToserver(ClientHttpRequestFactory requestFactory) A variant ofbindToserver()with a pre-configured request factory.- Returns:
- chained API to customize client config
-