Announcement Announcement Module
Collapse
No announcement yet.
Test controllers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Test controllers

    What tools work well for testing controller classes that rely on HttpServletRequest objects? In fact, if someone could post a JUnit test for a controller that a) sets up the spring context correctly outside of the web framework, and b) tests methods that make use of an HttpServletRequest object I would really appreciate it. Thanks in advance!

    Brian

  • #2
    There are a number of ways to do this. I think the best way to go ahead is using the Spring mock library. It's included in the distribution and includes MockHttpServletRequest and -Response classes as well as some other mock classes for HTTP-related stuff.

    I think it's best to just instantiate your controller in your test, to avoid dependencies on the WebApplicationContext. Have a look at org.springframework.web.servlet.mvc.FormController TestSuite. It tests the form controller with the mock classes.

    If you really want to have a WebApplicationContext in your tests, use the XmlWebApplicationContext. Normally, it takes its configuration files from the WEB-INF directory, but you can override this (have a look at the JavaDoc).

    Hope this helps a bit.

    Comment


    • #3
      That helps a lot. Thanks Alef!

      Brian

      Comment

      Working...
      X