1

I'm currently using Spring MVC's HandlerAdapter to test the annotations of my Spring controller actions to make sure the path variables, request parameters, session attributes, @Valid, etc. are being written correctly.

I've come across a problem where if a controller action has @ResponseBody and doesn't return a ModelAndView, I cannot test the response using the HandlerAdapter.

I found a question on this site that is related, but the accepted answer given is not satisfactory:

How to unit test a ResponseBody or ResponseEntity sent by a spring mvc Controller?

How can I test the annotations and the results at the same time? Or do I need to write two tests - one to test the annotations, and another to call the controller method directly?

Thanks

Community
  • 1
  • 1
egervari
  • 22,372
  • 32
  • 121
  • 175

2 Answers2

1

If you have a @ResponseBody then the handler shouldn't return a ModelAndView, it should write directly to the Response using a MessageConvert.

One approach would be to intercept the Response and read it instead of reading the returned value.

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
  • Can you please elaborate? I am having a bit of difficulty with `@RequestBody` and `@ResponseBody` using the HandlerAdapter. I'd like to be able to test these annotations, as well as the annotations I can already test with the HandlerAdapter. – egervari Nov 19 '11 at 00:29
0

please have a look at this answer I just found to your very same question I had:

I found that very useful and I'm implementing this solution in my test code.

Community
  • 1
  • 1
emas
  • 668
  • 8
  • 17