Announcement Announcement Module
No announcement yet.
DispatcherServlet extension for integration tests Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • DispatcherServlet extension for integration tests

    Before using Spring MVC for my web applications I was Struts users. There was a very handy testing framework for testing Struts Action (MockStrutsTestCase). Basically, test case would take MockHttpServletRequest, parse it and execute it without rendering actual view. From a test case I could locate ActionForm used and request/response attributes. I was hoping to find similar thing in Spring. Actually I found all necessary methods in DispatcherServlet. Problem is that all methods, which I could potentially override for making sort of MockDispatchServlet are private. Namely, getHandler, render and so on.

    Is there any particular reason to make them private? Do you have any alternative ideas for making integration tests in Spring? I would like to be able to verify if correct controller was taken, proper view chosen and appropriate set of request/session/context attributes are available.


  • #2
    I had the same thoughts (about needing strutstest like testing for spring mvc) when I started using Spring. Since spring carries fewer container dependencies and lends itself to interface driven backends, testing using simple mocks really was pretty simple. Now my controllers are tested just like any POJO.

    For example:
      public void testNormalSubmit() throws Exception
        MockHttpServletRequest req = new MockHttpServletRequest();
        req.addParameter("search", "value");
        List expectedResults = new ArrayList();
        ModelAndView result = null;
        Map model = null;
        searchCriteria = new ConsumerSearchCriteria();
        daoControl.expectAndReturn(mockDAO.getConsumers(searchCriteria), expectedResults);
        // THE TEST
        result = ta.onSubmit(req, null, searchCriteria, null);
        assertNotNull("got a mav", result);
        assertEquals("got correct view name", "viewconsumers", result.getViewName());
        model = result.getModel();
        assertNotNull("got a model", model);
        assertTrue("model name correct", model.containsKey("consumers"));
        assertSame("got expected results", expectedResults, model.get("consumers"));
    The thing I do miss is the nifty side effect of the MockStrutsTestCase is the fact that your configuration (e.g., struts-config.xml) was also being tested. I haven't found an easy way to do that in Spring without deploying (Spring IDE asside). I wonder how hard it would be to create a springmvctest based on the very cool struts test case sources?