Maxim Eliseev
Maxim Eliseev

Reputation: 3504

Spring MVC unit tests - can I pass URL (with params) to controller?

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

Upvotes: 2

Views: 6921

Answers (3)

storm_buster
storm_buster

Reputation: 7568

@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
    }
}

Upvotes: 3

NimChimpsky
NimChimpsky

Reputation: 47290

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.

Upvotes: 0

Stefan Birkner
Stefan Birkner

Reputation: 24540

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

Upvotes: 0

Related Questions