Announcement Announcement Module
Collapse
No announcement yet.
unit testing DispatcherServlet rather than Controllers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • unit testing DispatcherServlet rather than Controllers

    Using Spring 1.1.2.

    I'm having a problem where my SimpleMappingExceptionResolver is not returning the right view after an exception occurs.

    I would like to debug it but to do this I need to write a unit test that will somehow call DispatcherServlet.doService using a MockHttpServletRequest and Response, after setting up whatever servlet container setup is required (hopefully nothing at all except for setting the servlet name somehow) and associating the servlet with an appropriate WebApplicationContext.

    Anybody try this before?
    Any ideas?

    Best regards,
    Assaf

  • #2
    Take a look at the Spring unit test for the DispatcherServlet: org.springframework.web.servlet.DispatcherServletT ests. The source code is included in the Spring distributiun (for instance in "C:\java\spring-framework-1.1.2\test\org\springframework\web\servlet" if you would have unzipped the Spring distribution to C:\java).

    Erwin

    Comment


    • #3
      Thanks Erwin, that pointed me in the right direction.

      For anybody else running into this, the following code was enough for me to test the DispatcherServlet:
      Code:
      public class DispatcherServletTestCase extends TestCase {
         ...
      	DispatcherServlet servlet;
      
      	protected void setUp() throws Exception {
      		ServletConfig servletConfig = new MockServletConfig(new MockServletContext(), "servletName");
      		servlet = new DispatcherServlet();
      		servlet.setContextClass(TestWebApplicationContext.class);
      		servlet.init(servletConfig);
      	}
         ...
      	public void testSomething() throws Exception {
      		// getMockHttpRequest creates a request with standard attributes, encoding, etc.
      		MockHttpServletRequest request = this.getMockHttpRequest("/blah/projet.action", "GET");
      		// getMockHttpResponse creates a standard response
      		MockHttpServletResponse response = this.getMockHttpResponse();
      		HttpSession session = request.getSession();
      		// set specific session attributes
      		session.setAttribute("blah", "blah");
      		// set specific request parameters
      		request.addParameter("id", "3");		
      		
      		servlet.service(request, response);
      		if (LOG.isDebugEnabled()) {
      			// log the entire response
      			LOG.debug(response.getContentAsString());
      		}
      		// Now, check that the expected results occurred
      		...
      	}
      }
      And the TestWebApplicationContext class looks like this:
      Code:
      public class TestWebApplicationContext extends ClassPathXmlApplicationContext
              implements ConfigurableWebApplicationContext {
      
          private ServletContext servletContext;
      
          public TestWebApplicationContext()
                  throws BeansException {
              super(new String[] {"applicationContext.xml",  "applicationContext-test.xml"});
          }
      
          public ServletContext getServletContext() {
              return this.servletContext;
          }
      
          public Theme getTheme(String themeName) {
              return null;
          }
          
          public void setConfigLocations(String[] configLocations) {
           }
          public void setNamespace(String namespace) {
          }
          public void setServletContext(ServletContext servletContext) {
              this.servletContext = servletContext;
           }
      }
      It may be basic, but it covered my needs.

      You don't get any visibility into the ModelAndView, as you would testing Controllers, but you do get an integration test which runs though all of your interceptors and runs the full DispatcherServlet.doService method, including error handling. Very useful for stepping through in debug mode too, if anything unexpected is happening on the Web which you can't reproduce any other way.

      Best regards,
      Assaf

      Comment

      Working...
      X