Results 1 to 5 of 5

Thread: Load ApplicationContext from Web Application

  1. #1

    Default Load ApplicationContext from Web Application

    I have a web application that runs in Tomcat. I configured my web.xml with:

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    I also have some Java classes (not-servlets) that require beans managed by Spring. I would inject them but I am working with another technology that is loading them for me (coming from Flex).

    Anyways here is the deal. I have a class, "SpringUtil" that provides static methods for regular Java objects (i.e. not servlets) to access beans. It loads the context as such:

    private static ApplicationContext ctx;
    static {
    final String[] contexts = {"../applicationContext.xml"};
    ctx = new ClassPathXmlApplicationContext(contexts);
    }


    This works fine in production, however, when I run my unit tests they are failing because apparently it can't find the "applicationContext.xml" during testing. How should I fix this? Should my unit test setup the classpath (seems strange to do).

  2. #2
    Join Date
    Sep 2004
    Posts
    602

    Default

    See section 8.3.7. Spring TestContext Framework of the Spring manual

    specifically:

    8.3.7.2. Context management and caching

    Kind of weird you can't use dependency injection....

  3. #3

    Default

    thanks for the help.

  4. #4

    Default

    So my package structure looks like:

    src/main/webapp/WEB-INF/applicationContext.xml

    My test class is in:

    src/test/java/com/acme/TestFoo

    I am trying to annotate my test class:

    @ContextConfiguration(locations={"/WEB-INF/applicationContext.xml"})
    public class TestFoo extends AbstractTestNGSpringContextTests {
    ...
    }

    That doesn't work, how do I get the path to be accurate without hard coding something like "c:\dev\blah\..."?

  5. #5

    Default

    try:
    Code:
    @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"})
    public class TestFoo extends AbstractTestNGSpringContextTests {
    ...
    }

Posting Permissions

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