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 underlyingRestClient
viaRestClient.Builder
.static interface
RestTestClient.MockMvcSetupBuilder<S extends RestTestClient.Builder<S>, M extends MockMvcBuilder>
Extension ofRestTestClient.Builder
for 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.Builder
for tests against а RouterFunction MockMvc setup.static interface
Extension ofRestTestClient.Builder
for 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.Builder
for tests against а WebAppContext MockMvc setup. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The 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 aRestTestClient
by providing an already initializedMockMvc
instance to use as the server.Begin creating aRestTestClient
with a WebApplicationContext MockMvc setup.bindToController
(Object... controllers) Begin creating aRestTestClient
with a Standalone MockMvc setup.bindToRouterFunction
(RouterFunction<?>... routerFunctions) Begin creating aRestTestClient
with 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 anExchangeResult
is 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
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
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 aRestTestClient
with a Standalone MockMvc setup. -
bindToRouterFunction
static RestTestClient.RouterFunctionSetupBuilder bindToRouterFunction(RouterFunction<?>... routerFunctions) Begin creating aRestTestClient
with a RouterFunction MockMvc setup. -
bindToApplicationContext
static RestTestClient.WebAppContextSetupBuilder bindToApplicationContext(WebApplicationContext context) Begin creating aRestTestClient
with a WebApplicationContext MockMvc setup. -
bindTo
Begin creating aRestTestClient
by providing an already initializedMockMvc
instance 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
A variant ofbindToServer()
with a pre-configured request factory.- Returns:
- chained API to customize client config
-