MockRestServiceServer (Spring Framework 3.2.1.RELEASE API)
Date: 2013-01-24

org.springframework.test.web.client

## Class MockRestServiceServer

- java.lang.Object
- - org.springframework.test.web.client.MockRestServiceServer

-     public class MockRestServiceServer
      extends Object

  Main entry point for client-side REST testing. Used for tests that involve direct or indirect (through client code) use of the `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`.

  Since:
    3.2
  Author:
    Craig Walls, Rossen Stoyanchev

- - ### Method Summary

    Methods
    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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### createServer

          public static MockRestServiceServer createServer(RestTemplate restTemplate)

      Create a `MockRestServiceServer` and set up the given `RestTemplate` with a mock `ClientHttpRequestFactory`.
      Parameters:
        `restTemplate` - the RestTemplate to set up for mock testing
      Returns:
        the created mock server

    - #### createServer

          public static MockRestServiceServer createServer(RestGatewaySupport restGateway)

      Create a `MockRestServiceServer` and set up the given `RestGatewaySupport` with a mock `ClientHttpRequestFactory`.
      Parameters:
        `restGateway` - the REST gateway to set up for mock testing
      Returns:
        the created mock server

    - #### expect

          public ResponseActions expect(RequestMatcher requestMatcher)

      Set up a new HTTP request expectation. The returned `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.

      Parameters:
        `requestMatcher` - a request expectation, see `MockRestRequestMatchers`
      Returns:
        used to set up further expectations or to define a response

    - #### verify

          public void verify()

      Verify that all expected requests set up via `expect(RequestMatcher)` were indeed performed.
      Throws:
        `AssertionError` - when some expectations were not met