PDA

View Full Version : Accessing the xxx-servlet.xml file from a MockServletContext


keibi
Jul 27th, 2005, 05:05 AM
Hi,

I've been trying to test my Controllers using the spring-mock library lately. However, accessing the master-servlet.xml file have not proved to be easy. In my internal work structure, I have the files that goes in the root of the WEB-INF folder in a folder called "meta", så the XML file normally resides there. When I tried to access it using "/meta/master-servlet.xml" as a path, it could not be found. I tested with a getRealPath("")-call in the test, and it defaulted to the classes folder. Using ../ in the setConfigLocations method or the MockServletContext constructor to traverse directories returned a null value, so that didn't work. For now, I have been able to work around it by keeping a copy of the master-servlet.xml file in the classes folder, but this is not really a long-term solution. Does anyone know what I can do to keep the xml file in my meta folder?

Test code:

package no.uib.ii.dpg.tests.controllers;

import junit.framework.TestCase;
import no.uib.ii.dpg.dpe.controllers.PresentationControll er;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.XmlWebAppl icationContext;

public class BaseControllerTest extends TestCase {

protected XmlWebApplicationContext ctx;
protected PresentationController controller;

/** Sets up the preconditions for all tests */
protected void setUp() throws Exception {
super.setUp();

String[] paths = {"/master-servlet.xml"};
ctx = new XmlWebApplicationContext();
ctx.setConfigLocations(paths);
ctx.setServletContext(new MockServletContext(""));
ctx.refresh();
}
}

anreasBsz
Jul 27th, 2005, 09:17 AM
hi!
i tried it the same way and don't know the answer; but i found a different solution ...

public static final XmlBeanFactory LISTS_SERVLET_FACTORY
= new XmlBeanFactory( new FileSystemResource("build/web/WEB-INF/conf/lists-servlet.xml") );
protected static final ApplicationContext WEB_CONTEXT
= new GenericWebApplicationContext( LISTS_SERVLET_FACTORY );
protected void setUp() throws Exception {
controller.setApplicationContext(WEB_CONTEXT);
}

keibi
Jul 27th, 2005, 10:12 AM
Thanks for replying! Could you explain a little bit better what your code does? I need to get the XmlWebApplicationContext object, and set the servlet context to a mock. Do you know how?

anreasBsz
Jul 27th, 2005, 10:20 AM
Could you explain a little bit better what your code does?
it just prepares the controller for a simple Test. Using a web-context which fits my needs. this is, the call of getWebApplicationContext() inside the controller works.
I need to ... set the servlet context to a mock. Do you know how?
if you really need the mock-context, i don't know the solution.

Steve O
Jul 27th, 2005, 09:51 PM
Not sure how your src directory is set up, but how it works for me (and should for you too) would be to have your class:

package no.uib.ii.dpg.controllers;

import junit.framework.TestCase;
import no.uib.ii.dpg.dpe.controllers.PresentationControll er;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.XmlWebAppl icationContext;

public class BaseControllerTest extends TestCase {

protected XmlWebApplicationContext ctx;
protected PresentationController controller;

/** Sets up the preconditions for all tests */
protected void setUp() throws Exception {
super.setUp();

String[] paths = {"/WEB-INF/applicationContext*.xml"};
{"/WEB-INF/master-servlet.xml"};
ctx = new XmlWebApplicationContext();
ctx.setConfigLocations(paths);
ctx.setServletContext(new MockServletContext(""));
ctx.refresh();
}
}


My test classes reside in the "/test/" directory, NOT the "/src/" directory and I have my XML's in the "/test/WEB-INF/" directory.

You should be able to change "WEB-INF" to "meta" and copy your *.xml files in that directory.

I hope this helps, if your tests are not working still, post a test example and we'll figure it out from there!

Steve

keibi
Jul 29th, 2005, 08:08 AM
package no.uib.ii.dpg.dpe.controllers;

import junit.framework.TestCase;

import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.XmlWebAppl icationContext;

public class BaseControllerTest extends TestCase {

XmlWebApplicationContext ctx;
PresentationController controller;

/* Sets up the preconditions for all tests */
protected void setUp() throws Exception {
super.setUp();

String[] paths = {"/meta/master-servlet.xml"};
ctx = new XmlWebApplicationContext();
ctx.setConfigLocations(paths);
ctx.setServletContext(new MockServletContext(""));
ctx.refresh();
}

public void testTest() throws Exception {

assertTrue(1==1);
}
}


Running this test (via Eclipse) gives me this error:

INFO - Couldn't open InputStream for class path resource [meta/master-servlet.xml]
java.io.FileNotFoundException: class path resource [meta/master-servlet.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getI nputStream(ClassPathResource.java:127)
at org.springframework.mock.web.MockServletContext.ge tResourceAsStream(MockServletContext.java:167)
at org.springframework.web.context.support.ServletCon textResource.getInputStream(ServletContextResource .java:94)
at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:144)
at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:99)
at org.springframework.web.context.support.XmlWebAppl icationContext.loadBeanDefinitions(XmlWebApplicati onContext.java:115)
at org.springframework.web.context.support.XmlWebAppl icationContext.loadBeanDefinitions(XmlWebApplicati onContext.java:83)
at org.springframework.context.support.AbstractRefres hableApplicationContext.refreshBeanFactory(Abstrac tRefreshableApplicationContext.java:87)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:262)
at org.springframework.web.context.support.AbstractRe freshableWebApplicationContext.refresh(AbstractRef reshableWebApplicationContext.java:134)
at no.uib.ii.dpg.dpe.controllers.BaseControllerTest.s etUp(BaseControllerTest.java:22)
at junit.framework.TestCase.runBare(TestCase.java:125 )
at junit.framework.TestResult$1.protect(TestResult.ja va:106)
at junit.framework.TestResult.runProtected(TestResult .java:124)
at junit.framework.TestResult.run(TestResult.java:109 )
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:194)


In cleartext: the file does not exist. My catalogue structure looks like this:

Project
|-classes
|-src
|-tests
|-meta
|-lib



etc. The master-servlet.xml file resides in "/meta". The test resides in "/test/[package structure]".

Any ideas? I'm out of them.

Steve O
Jul 29th, 2005, 03:25 PM
I'm fresh out of ideas too. It looks like you do have a "/test/meta/*servlet.xml" - so I don't know where else to look.

I don't use Eclipse (I use Gel) to know the nuances of their classpaths... I'm not sure how well ANT integrates, but that may be an answer for you is to just run as an ANT task.

Maybe an Eclipse user may be able to give you better insight to if it is a classpath issue.

Good Luck,

Steve

keibi
Jul 31st, 2005, 03:37 AM
Thanks for trying to help out anyway, Steve. It's much appriciated.

jonnio
Jun 12th, 2007, 02:45 PM
I know this thread is old but I just solved the same problem with:

public void setUp() throws Exception {
super.setUp();
String[] contexts = new String[]{"/WEB-INF/blah-servlet-config.xml",
"/WEB-INF/blah-webflow-config.xml",
"/WEB-INF/flows/blah-flow-beans.xml"
};
ctx = new XmlWebApplicationContext();
ctx.setConfigLocations( contexts );
ctx.setServletContext( new MockServletContext( new FileSystemResourceLoader() ) );
ctx.refresh();
}


Note that the resource loader is specified on the servlet context.