Results 1 to 5 of 5

Thread: Using XmlWebApplicationContext in a web jUnit test

  1. #1
    Join Date
    Aug 2004
    Location
    Asheville, NC
    Posts
    3

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Asheville, NC
    Posts
    3

    Default 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?

  4. #4
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

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

  5. #5
    Join Date
    Aug 2004
    Location
    Asheville, NC
    Posts
    3

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

Similar Threads

  1. Replies: 32
    Last Post: Feb 2nd, 2010, 12:20 PM
  2. How can I test authorization in Junit?
    By j2emmy in forum Security
    Replies: 16
    Last Post: Mar 21st, 2006, 05:23 PM
  3. ApplicationContext returns null in Junit Test Case
    By penku in forum Spring-Modules
    Replies: 2
    Last Post: Sep 17th, 2005, 01:28 PM
  4. Test with JUnit
    By cecaldas in forum Data
    Replies: 3
    Last Post: Aug 9th, 2005, 10:03 PM
  5. Junit test failure
    By mhazer in forum Data
    Replies: 1
    Last Post: May 2nd, 2005, 02:37 AM

Posting Permissions

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