Spring Framework
  1. Spring Framework
  2. SPR-7951

Add first class support for client-side REST tests

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Major Major
    • Resolution: Complete
    • Affects Version/s: 3.1 M1
    • Fix Version/s: 3.2 RC1
    • Component/s: Test, Web
    • Labels:
    • Last commented by a User:
      false

      Description

      This is based on the spring-test-mvc project that has been available on Github, as a separate project, for over a year. The project provides a fluent API for defining expected requests and setting the desired responses, as well as verifying that all expectations were met.

      
      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();
      

        Issue Links

          Activity

          Hide
          Craig Walls added a comment -

          Update: I've moved the mock server framework code out of Spring Social's core module and into spring-social-test.

          Show
          Craig Walls added a comment - Update: I've moved the mock server framework code out of Spring Social's core module and into spring-social-test.
          Hide
          Rossen Stoyanchev added a comment -

          Craig, the spring-test-mvc project on Github was created to develop client and server-side test support before moving it to Spring trunk. Currently it has server-side support under org.springframework.test.web.server. We can start the client-side support with a copy of your code under org.springframework.test.web.client. I think you have permissions there, feel free to add it.

          Show
          Rossen Stoyanchev added a comment - Craig, the spring-test-mvc project on Github was created to develop client and server-side test support before moving it to Spring trunk. Currently it has server-side support under org.springframework.test.web.server . We can start the client-side support with a copy of your code under org.springframework.test.web.client . I think you have permissions there, feel free to add it.
          Hide
          Alessandro Bortolussi added a comment -

          Hi, I have found the mock server framework code extremely useful for testing RestTemplate, and I have introduced it in my company's test code.

          As Rossen mentioned, I also believe it would make really lots of sense to have it in a more general-purpose module, like spring-test-mvc.

          Show
          Alessandro Bortolussi added a comment - Hi, I have found the mock server framework code extremely useful for testing RestTemplate, and I have introduced it in my company's test code. As Rossen mentioned, I also believe it would make really lots of sense to have it in a more general-purpose module, like spring-test-mvc.
          Hide
          William Gorder added a comment -

          I also find this very useful and would like to see it added to spring-mvc. Is this still being worked in? I do have one suggestion though I usually wire my beans by interface not implementation I would like to see something more like this:
          'public static MockRestServiceServer createServer(RestOperations restOperations)' I had to change that to use it properly in my code I don't put getters for the RestTemplate in my code like was done in spring-social.

          Also you might want to run sonar/pmd/findbugs etc as it raised some flags on some random things like failure to close a BufferedReader in the ResponseCreators class.

          Thanks for your work on this Craig!

          Show
          William Gorder added a comment - I also find this very useful and would like to see it added to spring-mvc. Is this still being worked in? I do have one suggestion though I usually wire my beans by interface not implementation I would like to see something more like this: 'public static MockRestServiceServer createServer(RestOperations restOperations)' I had to change that to use it properly in my code I don't put getters for the RestTemplate in my code like was done in spring-social. Also you might want to run sonar/pmd/findbugs etc as it raised some flags on some random things like failure to close a BufferedReader in the ResponseCreators class. Thanks for your work on this Craig!
          Hide
          Rossen Stoyanchev added a comment -

          The project is currently located at https://github.com/SpringSource/spring-test-mvc. The plan is to move the code to the spring-test module for Spring 3.2.

          Show
          Rossen Stoyanchev added a comment - The project is currently located at https://github.com/SpringSource/spring-test-mvc . The plan is to move the code to the spring-test module for Spring 3.2.

            People

            • Assignee:
              Rossen Stoyanchev
              Reporter:
              Craig Walls
              Last updater:
              Chris Beams
            • Votes:
              6 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                2 years, 1 week, 5 days ago