public class MockRestServiceServer extends Object
RestTemplate
. Provides a way to set up fine-grained expectations
on the requests that will be performed through the RestTemplate
and
a way to define the responses to send back removing the need for an
actual running server.
Below is an example:
RestTemplate restTemplate = new RestTemplate() MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate); mockServer.expect(requestTo("/hotels/42")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("{ \"id\" : \"42\", \"name\" : \"Holiday Inn\"}", MediaType.APPLICATION_JSON)); Hotel hotel = restTemplate.getForObject("/hotels/{id}", Hotel.class, 42); // Use the hotel instance... mockServer.verify();
To create an instance of this class, use createServer(RestTemplate)
and provide the RestTemplate
to set up for the mock testing.
After that use expect(RequestMatcher)
and fluent API methods
andExpect(RequestMatcher)
and
andRespond(ResponseCreator)
to set up request expectations and responses, most likely relying on the default
RequestMatcher
implementations provided in MockRestRequestMatchers
and the ResponseCreator
implementations provided in
MockRestResponseCreators
both of which can be statically imported.
At the end of the test use verify()
to ensure all expected
requests were actually performed.
Note that because of the fluent API offered by this class (and related classes), you can typically use the Code Completion features (i.e. ctrl-space) in your IDE to set up the mocks.
Credits: The client-side REST testing support was
inspired by and initially based on similar code in the Spring WS project for
client-side tests involving the WebServiceTemplate
.
Modifier and Type | Method and Description |
---|---|
static MockRestServiceServer |
createServer(RestGatewaySupport restGateway)
Create a
MockRestServiceServer and set up the given
RestGatewaySupport with a mock ClientHttpRequestFactory . |
static MockRestServiceServer |
createServer(RestTemplate restTemplate)
Create a
MockRestServiceServer and set up the given
RestTemplate with a mock ClientHttpRequestFactory . |
ResponseActions |
expect(RequestMatcher requestMatcher)
Set up a new HTTP request expectation.
|
void |
verify()
Verify that all expected requests set up via
expect(RequestMatcher) were indeed performed. |
public static MockRestServiceServer createServer(RestTemplate restTemplate)
MockRestServiceServer
and set up the given
RestTemplate
with a mock ClientHttpRequestFactory
.restTemplate
- the RestTemplate to set up for mock testingpublic static MockRestServiceServer createServer(RestGatewaySupport restGateway)
MockRestServiceServer
and set up the given
RestGatewaySupport
with a mock ClientHttpRequestFactory
.restGateway
- the REST gateway to set up for mock testingpublic ResponseActions expect(RequestMatcher requestMatcher)
ResponseActions
is used to set up further expectations and to define the response.
This method may be invoked multiple times before starting the test, i.e. before
using the RestTemplate
, to set up expectations for multiple requests.
requestMatcher
- a request expectation, see MockRestRequestMatchers
public void verify()
expect(RequestMatcher)
were indeed performed.AssertionError
- when some expectations were not met