Skip to content

Add first class support for client-side REST tests [SPR-7951] #12606

Closed

Description

@spring-projects-issues

Craig Walls opened SPR-7951 and commented

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

Affects: 3.1 M1

Issue Links:

Referenced from: commits 22bcb54, 2638dd5

6 votes, 10 watchers

Activity

  1. spring-projects-issues commented on Feb 24, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Craig Walls commented

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

  2. spring-projects-issues commented on Jun 3, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    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.

  3. spring-projects-issues commented on Jun 24, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Alessandro Bortolussi commented

    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.

  4. spring-projects-issues commented on Apr 9, 2012

    @spring-projects-issues
    CollaboratorAuthor

    William Gorder commented

    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!

  5. spring-projects-issues commented on Apr 10, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    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.

  6. added
    in: test
    type: enhancement
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: testin: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions