Testing Spring MVC annotations mapppings

Using Spring MVC, you can specify that a specific URL will be processed by a specific method, and you can specify that specific parameters will be mapped to specific arguments, for example:

@Controller public class ImageController { @RequestMapping("/getImage") public String getImage( @RequestParam("imageId") int imageId, Map<String,Object> model ) { model.put("image",ImageService.getImage(imageId)); } } 

This is good and good, but now I want to check that the HTTP request with the imageId parameter will call this method correctly. In other words, I want a test that breaks if I delete or modify any annotations. Is there any way to do this?

It is easy to verify that getImage is working correctly. I could just create an ImageController and call getImage with the appropriate arguments. However, this is only half the test. The other half of the test should be whether getImage () will be called by the Spring framework when the corresponding HTTP request arrives. I feel that I also need a test for this part, especially since my @RequestMapping annotations become more complex and cause complex parameter parameters.

Could you show me a test that will break if I delete line 4, @RequestMapping("getImage") ?

+17
java spring spring-mvc
May 14 '09 at
source share
2 answers

You can use AnnotationMethodHandlerAdapter and its handle method programmatically. This will solve the method for this request and execute it. Unfortunately, this is a bit indirect. Actually, AMHA has a private class called ServletHandlerMethodResolver , which is responsible for a simple method solution for this request. I just filed a request for improvement on this topic, as I really would like it to be possible.

In the meantime, you can use, for example, EasyMock to create the layout of your controller class, expect this method to be called, and pass it to handle .

Controller:

 @Controller public class MyController { @RequestMapping("/users") public void foo(HttpServletResponse response) { // your controller code } } 

Test:

 public class RequestMappingTest { private MockHttpServletRequest request; private MockHttpServletResponse response; private MyController controller; private AnnotationMethodHandlerAdapter adapter; @Before public void setUp() { controller = EasyMock.createNiceMock(MyController.class); adapter = new AnnotationMethodHandlerAdapter(); request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); } @Test public void testname() throws Exception { request.setRequestURI("/users"); controller.foo(response); EasyMock.expectLastCall().once(); EasyMock.replay(controller); adapter.handle(request, response, controller); EasyMock.verify(controller); } } 

Regards, Ollie

+11
May 15 '09 at 18:24
source share

Ollie's solution covers testing a specific example of annotation, but what about the broader question of how to test all the other various MV7 annotations. My approach (which can be easily extended to other annotations) will be

 import static org.springframework.test.web.ModelAndViewAssert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({/* include live config here eg "file:web/WEB-INF/application-context.xml", "file:web/WEB-INF/dispatcher-servlet.xml" */}) public class MyControllerIntegrationTest { @Inject private ApplicationContext applicationContext; private MockHttpServletRequest request; private MockHttpServletResponse response; private HandlerAdapter handlerAdapter; private MyController controller; @Before public void setUp() { request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); handlerAdapter = applicationContext.getBean(HandlerAdapter.class); // I could get the controller from the context here controller = new MyController(); } @Test public void testFoo() throws Exception { request.setRequestURI("/users"); final ModelAndView mav = handlerAdapter.handle(request, response, controller); assertViewName(mav, null); assertAndReturnModelAttributeOfType(mav, "image", Image.class); } } 

I also wrote a blog entry about integrating Spring testing MVC annotation testing integration .

+6
Mar 16 '10 at 20:25
source share



All Articles