Class MockRestServiceServer
- java.lang.Object
- org.springframework.test.web.client.MockRestServiceServer
public final class MockRestServiceServer extends Object
Main entry point for client-side REST testing. Used for tests that involve direct or indirect use of theRestTemplate. Provides a way to set up expected requests that will be performed through theRestTemplateas 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, andExpectedCount: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
MockMvcClientHttpRequestFactoryon aRestTemplatewhich allows executing requests against an instance ofMockMvc.- Since:
- 3.2
- Author:
- Craig Walls, Rossen Stoyanchev
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMockRestServiceServer.MockRestServiceServerBuilderBuilder to create aMockRestServiceServer.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static MockRestServiceServer.MockRestServiceServerBuilderbindTo(AsyncRestTemplate asyncRestTemplate)Deprecated.see deprecation notice onAsyncRestTemplateitselfstatic MockRestServiceServer.MockRestServiceServerBuilderbindTo(RestTemplate restTemplate)Return a builder for aMockRestServiceServerthat should be used to reply to the givenRestTemplate.static MockRestServiceServer.MockRestServiceServerBuilderbindTo(RestGatewaySupport restGatewaySupport)Return a builder for aMockRestServiceServerthat should be used to reply to the givenRestGatewaySupport.static MockRestServiceServercreateServer(AsyncRestTemplate asyncRestTemplate)Deprecated.see deprecation notice onAsyncRestTemplateitselfstatic MockRestServiceServercreateServer(RestTemplate restTemplate)A shortcut forbindTo(restTemplate).build().static MockRestServiceServercreateServer(RestGatewaySupport restGateway)A shortcut forbindTo(restGateway).build().ResponseActionsexpect(ExpectedCount count, RequestMatcher matcher)An alternative toexpect(RequestMatcher)that also indicates how many times the request is expected to be executed.ResponseActionsexpect(RequestMatcher matcher)Set up an expectation for a single HTTP request.voidreset()Reset the internal state removing all expectations and recorded requests.voidverify()Verify that all expected requests set up viaexpect(RequestMatcher)were indeed performed.
Method Detail
expect
public ResponseActions expect(RequestMatcher matcher)
Set up an expectation for a single HTTP request. The returnedResponseActionscan 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
RestTemplatein order to set up all expected requests.- Parameters:
matcher- request matcher- Returns:
- a representation of the expectation
expect
public ResponseActions expect(ExpectedCount count, RequestMatcher matcher)
An alternative toexpect(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.
- Parameters:
count- the expected countmatcher- request matcher- Returns:
- a representation of the expectation
- Since:
- 4.3
verify
public void verify()
Verify that all expected requests set up viaexpect(RequestMatcher)were indeed performed.- Throws:
AssertionError- when some expectations were not met
reset
public void reset()
Reset the internal state removing all expectations and recorded requests.
bindTo
public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(RestTemplate restTemplate)
Return a builder for aMockRestServiceServerthat should be used to reply to the givenRestTemplate.- Since:
- 4.3
bindTo
@Deprecated public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(AsyncRestTemplate asyncRestTemplate)
Deprecated.see deprecation notice onAsyncRestTemplateitselfReturn a builder for aMockRestServiceServerthat should be used to reply to the givenAsyncRestTemplate.- Since:
- 4.3
bindTo
public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(RestGatewaySupport restGatewaySupport)
Return a builder for aMockRestServiceServerthat should be used to reply to the givenRestGatewaySupport.- Since:
- 4.3
createServer
public static MockRestServiceServer createServer(RestTemplate restTemplate)
A shortcut forbindTo(restTemplate).build().- Parameters:
restTemplate- the RestTemplate to set up for mock testing- Returns:
- the mock server
createServer
@Deprecated public static MockRestServiceServer createServer(AsyncRestTemplate asyncRestTemplate)
Deprecated.see deprecation notice onAsyncRestTemplateitselfA shortcut forbindTo(asyncRestTemplate).build().- Parameters:
asyncRestTemplate- the AsyncRestTemplate to set up for mock testing- Returns:
- the created mock server
createServer
public static MockRestServiceServer createServer(RestGatewaySupport restGateway)
A shortcut forbindTo(restGateway).build().- Parameters:
restGateway- the REST gateway to set up for mock testing- Returns:
- the created mock server