Results 1 to 3 of 3

Thread: Test controllers

  1. #1
    Join Date
    Sep 2004
    Posts
    19

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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.

  3. #3
    Join Date
    Sep 2004
    Posts
    19

    Default

    That helps a lot. Thanks Alef!

    Brian

Similar Threads

  1. help with testing controllers
    By pir8ped in forum Web
    Replies: 9
    Last Post: Apr 28th, 2006, 06:13 PM
  2. Replies: 5
    Last Post: Aug 1st, 2005, 06:30 PM
  3. Base Unit Test for Controllers?
    By ryan.tyer in forum Web
    Replies: 1
    Last Post: Apr 28th, 2005, 06:34 PM
  4. Portlet Form Controllers
    By johnalewis in forum Web
    Replies: 4
    Last Post: Mar 11th, 2005, 10:26 AM
  5. Test Controllers
    By kantorn in forum Web
    Replies: 11
    Last Post: Jan 2nd, 2005, 08:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •