Announcement Announcement Module
Collapse
No announcement yet.
Load ApplicationContext from Web Application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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....

    Comment


    • #3
      thanks for the help.

      Comment


      • #4
        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\..."?

        Comment


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

          Comment

          Working...
          X