PDA

View Full Version : Test controllers


brianstclair
Sep 26th, 2004, 04:39 PM
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

Alef Arendsen
Sep 26th, 2004, 06:11 PM
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.

brianstclair
Sep 26th, 2004, 06:32 PM
That helps a lot. Thanks Alef!

Brian