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 interfaceSpec for expectations on the response body content.static interfaceRestTestClient.BodySpec<B, S extends RestTestClient.BodySpec<B,S>> Spec for expectations on the response body decoded to a single Object.static interfaceRestTestClient.Builder<B extends RestTestClient.Builder<B>>Steps to customize the underlyingRestClientviaRestClient.Builder.static interfaceRestTestClient.MockMvcSetupBuilder<S extends RestTestClient.Builder<S>, M extends MockMvcBuilder>Extension ofRestTestClient.Builderfor tests against a MockMvc server.static interfaceSpecification for providing body of a request.static interfaceSpecification for providing the body and the URI of a request.static interfaceSpecification for adding request headers and performing an exchange.static interfaceSpecification for providing request headers and the URI of a request.static interfaceChained API for applying assertions to a response.static interfaceExtension ofRestTestClient.Builderfor tests витх аRouterFunction MockMvc setup.static interfaceExtension ofRestTestClient.Builderfor tests витх аstandalone MockMvc setup.static interfaceRestTestClient.UriSpec<S extends RestTestClient.RequestHeadersSpec<?>>Specification for providing the URI of a request.static interfaceExtension ofRestTestClient.Builderfor tests витх а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.Begin creating aRestTestClientwith aMockMvcBuilders.webAppContextSetup(org.springframework.web.context.WebApplicationContext)WebAppContext MockMvc setup}.bindToController(Object... controllers) Begin creating aRestTestClientwith aStandalone MockMvc setup.bindToRouterFunction(RouterFunction<?>... routerFunctions) Begin creating aRestTestClientwith aMockMvcBuilders.routerFunctions(org.springframework.web.servlet.function.RouterFunction<?>...)RouterFunction's 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 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 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
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 aRestTestClientwith aStandalone MockMvc setup. -
bindToRouterFunction
static RestTestClient.RouterFunctionSetupBuilder bindToRouterFunction(RouterFunction<?>... routerFunctions) Begin creating aRestTestClientwith aMockMvcBuilders.routerFunctions(org.springframework.web.servlet.function.RouterFunction<?>...)RouterFunction's MockMvc setup}. -
bindToApplicationContext
static RestTestClient.WebAppContextSetupBuilder bindToApplicationContext(WebApplicationContext context) Begin creating aRestTestClientwith aMockMvcBuilders.webAppContextSetup(org.springframework.web.context.WebApplicationContext)WebAppContext 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
A variant ofbindToServer()with a pre-configured request factory.- Returns:
- chained API to customize client config
-