Announcement Announcement Module
Collapse
No announcement yet.
Using XmlWebApplicationContext in a web jUnit test Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using XmlWebApplicationContext in a web jUnit test

    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.

    Code:
    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.

    Code:
    Aug 25, 2004 4:32:39 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader 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
    Code:
    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?

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

    Comment


    • #3
      More web testing fun...

      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.

      Code:
      mockServletContext.setAttribute&#40;WebApplicationContext.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?

      Comment


      • #4
        Re: More web testing fun...

        Code:
        mockServletContext.setAttribute&#40;WebApplicationContext.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.

        Comment


        • #5
          Auto wiring...

          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.

          Comment

          Working...
          X