Spring MVC unit tests - can I pass URL (with param

2019-04-02 16:23发布

问题:

I am going to unit test a Spring MVC controller (or whole site if possible).

I want to pass a URL (as a string), e.g. "/metrics/filter?a1=1&a2=2&a3=abcdefg&wrongParam=WRONG" and check what controller will return.

Is there an easy way to do it?

Example:

 @RequestMapping(value = {"/filter"}, method = RequestMethod.GET)
    @ResponseBody
    public List<MetricType> getMetricTypes(
             @RequestParam(value = "subject", required = false) Long subjectId
            ,@RequestParam(value = "area", required = false) Long areaId
            ,@RequestParam(value = "onlyImmediateChildren", required = false) Boolean onlyImmediateChildren

            ,@RequestParam(value = "componentGroup", required = false) Long componentGroupId

            ,@RequestParam(value = "hasComponentGroups", required = false) Boolean hasComponentGroups
                                            ) throws Exception
    {
          //some code
    }

Many thanks

Maxim

UPDATED

  • I only use GET, not post
  • I do not use model objects (see example above)
  • My system is a web service which has a lot of "/someEntity/filter?a1=123&a2=1234&a3=etc" method with various combinations of parameters. I am not sure it is practical to use model objects in this case.

回答1:

@RunWith( SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:WebRoot/WEB-INF/path/to/your-context.xml") 
public class YourControllerTest {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private AnnotationMethodHandlerAdapter adapter;


@Before
public void setUp(){
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
    this.adapter = new AnnotationMethodHandlerAdapter();
}


    @Test
    public void getMetricTypes() throws Exception{



        request.setRequestURI("/filter");
        request.setMethod("GET");
        request.setParameter("subject", "subject");
        request.setParameter("area", "area");    
        request.setParameter("onlyImmediateChildren", "onlyImmediateChildren");    
        request.setParameter("componentGroup", "componentGroup");    
        request.setParameter("hasComponentGroups", "hasComponentGroups");    

        ModelAndView mav = adapter.handle(request, response, yourController);
        Assert.assertEquals(200, response.getStatus());
        //Assert what you want
    }
}


回答2:

A new and easier way for testing Spring MVC controllers is spring-test-mvc.



回答3:

Most people recommend not integration testing the request mappings - you can unit test your controllers pojo methods: ie without any spring bindings. Whats the point of testing if Spring is working is the argument put forward.

So in your unit test call the controller method normally passing in an implementation of model as parameter (extendedmodelmap(?). The test code can then check what gets added to model, and also the return values.

If you really want to integration test spring-mvc this article is good.