public final class MockRestServiceServer extends Object
RestTemplate
. Provides a
way to set up expected requests that will be performed through the
RestTemplate
as well as mock responses to send back thus removing the
need for an actual server.
Below is an example that assumes static imports from
MockRestRequestMatchers
, MockRestResponseCreators
,
and ExpectedCount
:
RestTemplate restTemplate = new RestTemplate() MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build(); server.expect(manyTimes(), 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... // Verify all expectations met server.verify();
Note that as an alternative to the above you can also set the
MockMvcClientHttpRequestFactory
on a RestTemplate
which
allows executing requests against an instance of
MockMvc
.
Modifier and Type | Class and Description |
---|---|
static interface |
MockRestServiceServer.MockRestServiceServerBuilder
Builder to create a
MockRestServiceServer . |
Modifier and Type | Method and Description |
---|---|
static MockRestServiceServer.MockRestServiceServerBuilder |
bindTo(AsyncRestTemplate asyncRestTemplate)
Deprecated.
see deprecation notice on
AsyncRestTemplate itself |
static MockRestServiceServer.MockRestServiceServerBuilder |
bindTo(RestGatewaySupport restGatewaySupport)
Return a builder for a
MockRestServiceServer that should be used
to reply to the given RestGatewaySupport . |
static MockRestServiceServer.MockRestServiceServerBuilder |
bindTo(RestTemplate restTemplate)
Return a builder for a
MockRestServiceServer that should be used
to reply to the given RestTemplate . |
static MockRestServiceServer |
createServer(AsyncRestTemplate asyncRestTemplate)
Deprecated.
see deprecation notice on
AsyncRestTemplate itself |
static MockRestServiceServer |
createServer(RestGatewaySupport restGateway)
A shortcut for
bindTo(restGateway).build() . |
static MockRestServiceServer |
createServer(RestTemplate restTemplate)
A shortcut for
bindTo(restTemplate).build() . |
ResponseActions |
expect(ExpectedCount count,
RequestMatcher matcher)
An alternative to
expect(RequestMatcher) that also indicates how
many times the request is expected to be executed. |
ResponseActions |
expect(RequestMatcher matcher)
Set up an expectation for a single HTTP request.
|
void |
reset()
Reset the internal state removing all expectations and recorded requests.
|
void |
verify()
Verify that all expected requests set up via
expect(RequestMatcher) were indeed performed. |
public ResponseActions expect(RequestMatcher matcher)
ResponseActions
can be used to set up further expectations as
well as to define the response.
This method may be invoked any number times before starting to make
request through the underlying RestTemplate
in order to set up
all expected requests.
matcher
- request matcherpublic ResponseActions expect(ExpectedCount count, RequestMatcher matcher)
expect(RequestMatcher)
that also indicates how
many times the request is expected to be executed.
When request expectations have an expected count greater than one, only the first execution is expected to match the order of declaration. Subsequent request executions may be inserted anywhere thereafter.
count
- the expected countmatcher
- request matcherpublic void verify()
expect(RequestMatcher)
were indeed performed.AssertionError
- when some expectations were not metpublic void reset()
public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(RestTemplate restTemplate)
MockRestServiceServer
that should be used
to reply to the given RestTemplate
.@Deprecated public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(AsyncRestTemplate asyncRestTemplate)
AsyncRestTemplate
itselfMockRestServiceServer
that should be used
to reply to the given AsyncRestTemplate
.public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(RestGatewaySupport restGatewaySupport)
MockRestServiceServer
that should be used
to reply to the given RestGatewaySupport
.public static MockRestServiceServer createServer(RestTemplate restTemplate)
bindTo(restTemplate).build()
.restTemplate
- the RestTemplate to set up for mock testing@Deprecated public static MockRestServiceServer createServer(AsyncRestTemplate asyncRestTemplate)
AsyncRestTemplate
itselfbindTo(asyncRestTemplate).build()
.asyncRestTemplate
- the AsyncRestTemplate to set up for mock testingpublic static MockRestServiceServer createServer(RestGatewaySupport restGateway)
bindTo(restGateway).build()
.restGateway
- the REST gateway to set up for mock testing