Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-projects-issues opened this issue Feb 9, 2011 · 5 comments
Closed
Assignees
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Feb 9, 2011

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:

  • #12650 MockHttpRequest to support ClientRequestInterceptor testing ("Cloned to")
  • #14493 Add documentation for the Spring MVC Test support
  • #14492 Add first class support for testing Spring MVC applications
  • #13849 Release 1.0 spring-test-mvc

Referenced from: commits 22bcb54, 2638dd5

6 votes, 10 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 24, 2011

Craig Walls commented

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

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jun 3, 2011

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.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jun 24, 2011

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.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 9, 2012

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!

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 10, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants