Hi all,
Imagine the following standard ant based web app layout.
I'm migrating tests from the old inheritance based spring test api to the new TestContext framework.Code:-- src -- tests -- webapp -- WEB-INF -- a.xml -- b.xml -- etc
The old tests extend TestCase and do something of this nature.
The reason the above is necessary is because there is code in the source directory which loads resources using paths like '/WEB-INF/blah.xml'. So the tests must also be relative to '/webapp' so that when the code runs it can find those resource paths.Code:ServletContext servletContext = new MockServletContext("/webapp", new FileSystemResourceLoader()); ConfigurableWebApplicationContext springContext = new XmlWebApplicationContext(); springContext.setServletContext(servletContext); springContext.setConfigLocations(new String[] {"/WEB-INF/a.xml", "/WEB-INF/b.xml"}); springContext.refresh();
The new TestContextFramework obviously recommends something like below.
I've also noticed that @ContextConfiguration has three arguments - inheritLocations, loader and locations.Code:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/WEB-INF/a.xml", "/WEB-INF/b.xml"}) public class MyTest { }
So my question is how can I use the TestContextFramework but also provide a MockServletContext with a base directory of /webapp so that all context file locations are relative to /webapp and can be specified like '/WEB-INF/my-context.xml'? Or phrased another way how can I make the test load the context relative to /webapp.
I've noticed that there is a loader argument to @ContextConfiguration. I've also seen certain posts by Sam Brannen suggesting that a custom ContextLoader can be implemented. Is this a use case for a custom ContextLoader? If so is it possible to provide an example in relation to this post? If not then how else can this be achieved cleanly?
Essentially when my root level test class loads the context for all other tests I want it to do so relative to /webapp. This has to work both with ant on command line and within eclipse.
Many thanks.


Reply With Quote
