PDA

View Full Version : Using XmlWebApplicationContext in a web jUnit test



jeff_duska
Aug 25th, 2004, 04:08 PM
I'm trying to test my command object from my web application. I need to setup the Spring context, so I've been following an example out of Matt Raible's Spring Live. I have added the following code to my setUp() of my test case.


String path[] = {"/src/tests/applicationContext.xml"};
ctx = new XmlWebApplicationContext();
ctx.setConfigLocations(path);
ctx.setServletContext(new MockServletContext(""));
ctx.refresh();

My problem is that no matter what I put in path, I get the following exception.


Aug 25, 2004 4:32:39 PM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
INFO: Loading XML bean definitions from resource [/../applicationContext.xml] of ServletContext
Aug 25, 2004 4:32:39 PM org.springframework.mock.web.MockServletContext getResourceAsStream
SEVERE: Couldn't open resource class path resource [../applicationContext.xml]
java.io.FileNotFoundException: Could not open class path resource [../applicationContext.xml]


Yet, when I change this to
ac = new FileSystemXmlApplicationContext("src/tests/applicationContext.xml");

It finds applicationContext.xml without a problem. Does anyone have a suggestion of what I'm doing wrong?

irbouho
Aug 25th, 2004, 04:16 PM
from javadoc
FileSystemXmlApplicationContext


* <p>Treats resource paths as file system resources, when using
* ApplicationContext.getResource. Resource paths are considered relative
* to the current VM working directory, even if they start with a slash.


XmlWebApplicationContext


* <p>Interprets resource paths as servlet context resources, i.e. as paths beneath
* the web application root. Absolute paths, e.g. for files outside the web app root,
* can be accessed via "file:" URLs, as implemented by AbstractApplicationContext.

jeff_duska
Aug 25th, 2004, 06:43 PM
Thanks, I was able to use that to solve the first problem. I still don't understand why it didn't like the relative path, but that's a problem for another day.

Right now, I'm trying to test my command objects. My object calls WebApplicationContext to get a couple of beans. Using Matt's example returns a NPE. Which after I thought about made sense, because I wasn't setting the Spring context into ServletContext.

I added the following code to Matt's example to get it working.


mockServletContext.setAttribute&#40;WebApplicationCont ext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,ctx&#41;;
mockSession = new MockHttpSession&#40;mockServletContext&#41;;
request.setSession&#40;mockSession&#41;;

My question is this the best way to do this?

mraible
Aug 25th, 2004, 07:54 PM
mockServletContext.setAttribute&#40;WebApplicationCont ext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,ctx&#41;;
mockSession = new MockHttpSession&#40;mockServletContext&#41;;
request.setSession&#40;mockSession&#41;;

My question is this the best way to do this?

Looks reasonable to me. Another (cleaner) option is to wire up your controllers by setting their dependencies. Then you wouldn't need access to the WebApplicationContext.

jeff_duska
Aug 25th, 2004, 09:02 PM
Thanks Matt. I plan to do that in the next revision. I've missed a deadline, so I need to get this to QA folks ASAP. I'm reading both Rod's and your book, so once I digest it all I'll rewrite the web-tier to use either Struts or the Spring MVC.