Announcement Announcement Module
Collapse
No announcement yet.
Accessing the xxx-servlet.xml file from a MockServletContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing the xxx-servlet.xml file from a MockServletContext

    Hi,

    I've been trying to test my Controllers using the spring-mock library lately. However, accessing the master-servlet.xml file have not proved to be easy. In my internal work structure, I have the files that goes in the root of the WEB-INF folder in a folder called "meta", så the XML file normally resides there. When I tried to access it using "/meta/master-servlet.xml" as a path, it could not be found. I tested with a getRealPath("")-call in the test, and it defaulted to the classes folder. Using ../ in the setConfigLocations method or the MockServletContext constructor to traverse directories returned a null value, so that didn't work. For now, I have been able to work around it by keeping a copy of the master-servlet.xml file in the classes folder, but this is not really a long-term solution. Does anyone know what I can do to keep the xml file in my meta folder?

    Test code:
    Code:
    package no.uib.ii.dpg.tests.controllers;
    
    import junit.framework.TestCase;
    import no.uib.ii.dpg.dpe.controllers.PresentationController;
    import org.springframework.mock.web.MockServletContext;
    import org.springframework.web.context.support.XmlWebApplicationContext;
    
    public class BaseControllerTest extends TestCase {
    
    	protected XmlWebApplicationContext ctx;
    	protected PresentationController controller;
    	
    	/** Sets up the preconditions for all tests */
    	protected void setUp() throws Exception {
    		super.setUp();
    		
    		String[] paths = {"/master-servlet.xml"};
    		ctx = new XmlWebApplicationContext();
    		ctx.setConfigLocations(paths);
    		ctx.setServletContext(new MockServletContext(""));
    		ctx.refresh();
    	}
    }

  • #2
    hi!
    i tried it the same way and don't know the answer; but i found a different solution ...

    public static final XmlBeanFactory LISTS_SERVLET_FACTORY
    = new XmlBeanFactory( new FileSystemResource("build/web/WEB-INF/conf/lists-servlet.xml") );
    protected static final ApplicationContext WEB_CONTEXT
    = new GenericWebApplicationContext( LISTS_SERVLET_FACTORY );
    protected void setUp() throws Exception {
    controller.setApplicationContext(WEB_CONTEXT);
    }

    Comment


    • #3
      Thanks for replying! Could you explain a little bit better what your code does? I need to get the XmlWebApplicationContext object, and set the servlet context to a mock. Do you know how?

      Comment


      • #4
        Originally posted by keibi
        Could you explain a little bit better what your code does?
        it just prepares the controller for a simple Test. Using a web-context which fits my needs. this is, the call of getWebApplicationContext() inside the controller works.
        Originally posted by keibi
        I need to ... set the servlet context to a mock. Do you know how?
        if you really need the mock-context, i don't know the solution.

        Comment


        • #5
          Not sure how your src directory is set up, but how it works for me (and should for you too) would be to have your class:
          Code:
          package no.uib.ii.dpg.controllers;
          
          import junit.framework.TestCase;
          import no.uib.ii.dpg.dpe.controllers.PresentationController;
          import org.springframework.mock.web.MockServletContext;
          import org.springframework.web.context.support.XmlWebApplicationContext;
          
          public class BaseControllerTest extends TestCase {
          
             protected XmlWebApplicationContext ctx;
             protected PresentationController controller;
             
             /** Sets up the preconditions for all tests */
             protected void setUp() throws Exception {
                super.setUp();
                
                String[] paths = {"/WEB-INF/applicationContext*.xml"};
                                        {"/WEB-INF/master-servlet.xml"};
                ctx = new XmlWebApplicationContext();
                ctx.setConfigLocations(paths);
                ctx.setServletContext(new MockServletContext(""));
                ctx.refresh();
             }
          }
          My test classes reside in the "/test/" directory, NOT the "/src/" directory and I have my XML's in the "/test/WEB-INF/" directory.

          You should be able to change "WEB-INF" to "meta" and copy your *.xml files in that directory.

          I hope this helps, if your tests are not working still, post a test example and we'll figure it out from there!

          Steve

          Comment


          • #6
            Code:
            package no.uib.ii.dpg.dpe.controllers;
            
            import junit.framework.TestCase;
            
            import org.springframework.mock.web.MockServletContext;
            import org.springframework.web.context.support.XmlWebApplicationContext;
            
            public class BaseControllerTest extends TestCase {
            	
            	XmlWebApplicationContext ctx;
            	PresentationController controller;
            	
            	/* Sets up the preconditions for all tests */ 
            	protected void setUp() throws Exception {
            		super.setUp();
            		
            		String[] paths = {"/meta/master-servlet.xml"};
            		ctx = new XmlWebApplicationContext();
            		ctx.setConfigLocations(paths);
            		ctx.setServletContext(new MockServletContext(""));
            		ctx.refresh();
            	}
            	
            	public void testTest() throws Exception {
            		
            		assertTrue(1==1);
            	}
            }
            Running this test (via Eclipse) gives me this error:
            Code:
            INFO - Couldn't open InputStream for class path resource [meta/master-servlet.xml]
            java.io.FileNotFoundException: class path resource [meta/master-servlet.xml] cannot be opened because it does not exist
            	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:127)
            	at org.springframework.mock.web.MockServletContext.getResourceAsStream(MockServletContext.java:167)
            	at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:94)
            	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:144)
            	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:99)
            	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:115)
            	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:83)
            	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:87)
            	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:262)
            	at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:134)
            	at no.uib.ii.dpg.dpe.controllers.BaseControllerTest.setUp(BaseControllerTest.java:22)
            	at junit.framework.TestCase.runBare(TestCase.java:125)
            	at junit.framework.TestResult$1.protect(TestResult.java:106)
            	at junit.framework.TestResult.runProtected(TestResult.java:124)
            	at junit.framework.TestResult.run(TestResult.java:109)
            	at junit.framework.TestCase.run(TestCase.java:118)
            	at junit.framework.TestSuite.runTest(TestSuite.java:208)
            	at junit.framework.TestSuite.run(TestSuite.java:203)
            	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
            	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
            	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
            In cleartext: the file does not exist. My catalogue structure looks like this:
            Code:
            Project
            |-classes
            |-src
            |-tests
            |-meta
            |-lib
            etc. The master-servlet.xml file resides in "/meta". The test resides in "/test/[package structure]".

            Any ideas? I'm out of them.

            Comment


            • #7
              I'm fresh out of ideas too. It looks like you do have a "/test/meta/*servlet.xml" - so I don't know where else to look.

              I don't use Eclipse (I use Gel) to know the nuances of their classpaths... I'm not sure how well ANT integrates, but that may be an answer for you is to just run as an ANT task.

              Maybe an Eclipse user may be able to give you better insight to if it is a classpath issue.

              Good Luck,

              Steve

              Comment


              • #8
                Thanks for trying to help out anyway, Steve. It's much appriciated.

                Comment


                • #9
                  I know this thread is old but I just solved the same problem with:

                  Code:
                      public void setUp() throws Exception {
                          super.setUp();
                          String[] contexts = new String[]{"/WEB-INF/blah-servlet-config.xml",
                                  "/WEB-INF/blah-webflow-config.xml",
                                  "/WEB-INF/flows/blah-flow-beans.xml"
                          };
                          ctx = new XmlWebApplicationContext();
                          ctx.setConfigLocations( contexts );
                          ctx.setServletContext( new MockServletContext( new FileSystemResourceLoader() ) );
                          ctx.refresh();
                      }
                  Note that the resource loader is specified on the servlet context.

                  Comment

                  Working...
                  X