6

When I do junit tests, I do something like this to test spring mvc controllers :

request.setRequestURI("/projects/"+idProject+"/modify");
ModelAndView mv = handlerAdapter.handle(request, response, controller);

where controller tested is like :

@RequestMapping(value = "{id}/modify")
public String content(ModelMap model, @PathVariable("id") Project object) {

But I don't find how to get the ResponseBody answer of request handlers defined like this :

@RequestMapping("/management/search")
public @ResponseBody ArrayList<SearchData> search(@RequestParam("q")) {
        ....
                ....
        ArrayList<SearchData> datas = ....;

        return datas;
    }
skaffman
  • 398,947
  • 96
  • 818
  • 769
Nico
  • 3,430
  • 4
  • 20
  • 27
  • looking around I found another solution that in my case is working: http://stackoverflow.com/questions/9138555/spring-framework-test-restful-web-service-controller-offline-i-e-no-server-n It seems very easy to implement and it fits nicely with my test code. – emas Feb 23 '12 at 15:03

1 Answers1

3

Your unit test only needs to verify the contents of the return value of the method:

ArrayList<SearchData> results = controller.search("value");
assertThat(results, ...)

The @ResponseBody annotation is irrelevant. This is one of the big benefits of annotated controllers - your unit tests can focus on the business logic, not the framework mechanics. With pre-annotation controllers, half of your test code is spent constructing mock requests, responses, and associated gubbins like that. It's a distraction.

Testing that your code's annotations integrate properly with the framework is the job of integration and/or functional tests.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Also, for a ResponseEntity result you can just call getBody to get the results. i.e. ResponseEntity results = controller.search("value"); MyObject obj = results.getBody(); assertThat( obj, ...) – digitaljoel Mar 22 '11 at 23:23
  • I hope it's not bad form to link to my own blog. In February I wrote entries directly related to testing controllers. http://digitaljoel.nerd-herders.com/2011/02/05/mock-testing-spring-mvc-controller/ and http://digitaljoel.nerd-herders.com/2011/02/05/using-mockito-to-test-spring-mvc-ajax-interaction/ with the latter testing a method that returns a ResponseEntity. – digitaljoel Mar 22 '11 at 23:30
  • Ok thank you for your answer skaffman, you're right I just need to verify the contents of the return value. – Nico Mar 23 '11 at 10:02
  • Thank you digitaljoel, I'll have a look at your blog it could be very interesting for me :) – Nico Mar 23 '11 at 10:08
  • 5
    I disagree with this answer. Testing the annotations, request parameters, etc. is very important. The author of the question was on the right track when he was using `HandlerAdapter` – egervari Nov 17 '11 at 23:53
  • 1
    @egervari: I didn't say it wasn't important, I said it wasn't the job of unit tests, as my last sentence says. – skaffman Nov 18 '11 at 07:31