Announcement Announcement Module
Collapse
No announcement yet.
Exception: Could not open ServletContext resource /WEB-INF/applicationContext.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception: Could not open ServletContext resource /WEB-INF/applicationContext.xml

    Hi
    I am using JUnit 3.8 for unit testing my code using Appfuse/Equinox framework. When i run BasePageTestCase.java it throws following exception
    Code:
    log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
    log4j:WARN Please initialize the log4j system properly.
    java.lang.ExceptionInInitializerError
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    	at java.lang.reflect.Constructor.newInstance(Unknown Source)
    	at junit.framework.TestSuite.createTest(TestSuite.java:131)
    	at junit.framework.TestSuite.addTestMethod(TestSuite.java:114)
    	at junit.framework.TestSuite.<init>(TestSuite.java:75)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.getTest(JUnit3TestLoader.java:102)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.loadTests(JUnit3TestLoader.java:59)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:180)
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:148)
    	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:129)
    	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:145)
    	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:126)
    	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
    	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:89)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:262)
    	at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:252)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:190)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
    	at com.boeing.nmt.nams.view.BasePageTestCase.<clinit>(BasePageTestCase.java:47)
    	... 13 more
    Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    	at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:99)
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:167)
    	... 25 more
    Here is the code in BasePageTestCase.java
    Code:
    package com.boeing.nmt.nams.view;
    import javax.faces.FactoryFinder;
    import javax.faces.application.Application;
    import javax.faces.application.ApplicationFactory;
    import javax.faces.context.FacesContext;
    import javax.faces.context.FacesContextFactory;
    import javax.faces.el.ValueBinding;
    import javax.faces.lifecycle.Lifecycle;
    import javax.faces.lifecycle.LifecycleFactory;
    import javax.faces.webapp.FacesServlet;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import junit.framework.TestCase;
    import org.apache.myfaces.webapp.StartupServletContextListener;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.mock.web.MockHttpServletResponse;
    import org.springframework.mock.web.MockServletConfig;
    import org.springframework.mock.web.MockServletContext;
    import org.springframework.web.context.ContextLoader;
    import org.springframework.web.context.ContextLoaderListener;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.jsf.FacesContextUtils;
    
    public class BasePageTestCase extends TestCase {
    
    	protected final Log log = LogFactory.getLog(getClass());
        protected static FacesContext facesContext;
        protected static MockServletConfig config;
        protected static MockServletContext servletContext;
        protected static WebApplicationContext ctx;
        
        //  This static block ensures that Spring's BeanFactory and JSF's 
        // FacesContext is only loaded once for all tests. 
        static {
            servletContext = new MockServletContext("");
            // This static block ensures that Spring's BeanFactory and JSF's 
            // FacesContext is only loaded once for all tests. 
            servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
                                            "WEB-INF/applicationContext.xml");
    
            ServletContextListener contextListener = new ContextLoaderListener();
            ServletContextEvent event = new ServletContextEvent(servletContext);
            contextListener.contextInitialized(event);
    
            config = new MockServletConfig(servletContext);
            facesContext = performFacesContextConfig();
            ctx = FacesContextUtils.getRequiredWebApplicationContext(facesContext);
        }
        
        protected static FacesContext performFacesContextConfig() {
            StartupServletContextListener facesListener =
                new StartupServletContextListener();
            ServletContextEvent event = new ServletContextEvent(servletContext);
            facesListener.contextInitialized(event);
    
            LifecycleFactory lifecycleFactory =
                (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
    
            Lifecycle lifecycle = lifecycleFactory.getLifecycle(getLifecycleId());
    
            FacesContextFactory facesCtxFactory =
                (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
    
            FacesContext ctx =
                facesCtxFactory.getFacesContext(servletContext,
                                                new MockHttpServletRequest(),
                                                new MockHttpServletResponse(),
                                                lifecycle);
    
            return ctx;
        }
    
        protected static String getLifecycleId() {
            String lifecycleId =
                servletContext.getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR);
    
            return (lifecycleId != null) ? lifecycleId
                                         : LifecycleFactory.DEFAULT_LIFECYCLE;
        }
    
        /**
         * Get managed bean based on the bean name.
         *
         * @param beanName the bean name
         * @return the managed bean associated with the bean name
         */
        protected Object getManagedBean(String beanName) {
            return getValueBinding(getJsfEl(beanName)).getValue(facesContext);
        }
    
        private Application getApplication() {
            ApplicationFactory appFactory =
                (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
    
            return appFactory.getApplication();
        }
    
        private ValueBinding getValueBinding(String el) {
            return getApplication().createValueBinding(el);
        }
    
        private String getJsfEl(String value) {
            return "#{" + value + "}";
        }
    
    
    }
    Any pointers/suggestions to resolve the error will be highly appreciated

    Regards
    Bansi

  • #2
    I guess this will be a problem with the working directory set when running the unit test. As you are specifying a relative path, you need to set this as the parent of WEB-INF.

    Comment


    • #3
      Thanks for quick response.
      You are right even i thought this might be due to working set directory but was Not Sure.

      Here is my directory structure
      -src
      |_ Source Code
      -test
      |_ BasePageTestCase.java
      |_ ManufacturerBean.java
      -WebRoot
      |_ WEB-INF
      |_ web.xml

      I changed the snippet in BasePageTestCase.java as following
      Code:
      servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
                                              "/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-hibernate.xml");
      Also a little correction to my earlier posting i am getting the error only when i run ManufacturerBean.java which calls super(). Here is the code
      Code:
      package com.boeing.nmt.nams.view;
      
      import com.boeing.nmt.nams.model.Manufacturer;
      import com.boeing.nmt.nams.service.ManufacturerManager;
      import com.boeing.nmt.nams.view.bean.ManufacturerBean;
      
      import junit.framework.TestCase;
      
      public class ManufacturerBeanTest extends BasePageTestCase {
      
      	private ManufacturerBean bean;
          private String manufId;
      
      	protected void setUp() throws Exception {
      		super.setUp();
      		bean = (ManufacturerBean) getManagedBean("manufacturerBean");
              // verify that "userForm" has been defined as a managed-bean
              assertNotNull("you need to define 'manufacturerBean' in faces-config.xml", bean);
              
              // create a new user
              Manufacturer manuf = new Manufacturer();
              manuf.setName("Tom");
              manuf.setDescription("Tom Corporation");
              manuf.setSource("CIIM");
              manuf.setStatus("In Active");
              
              
              // persist to database
              ManufacturerManager manufManager = (ManufacturerManager) ctx.getBean("manufManager");
              manufManager.createManufacturer(manuf);        
              manufId = manuf.getId().toString();
              
      	}
      
      	protected void tearDown() throws Exception {
      		super.tearDown();
      		 bean = null;
      	}
      	
      	public void testCreateManufacturer() throws Exception {
      	       // bean.setId(manufId);
      	        assertNotNull(bean.getManufacturer());
      	        bean.getManufacturer().setName("Jack");	        
      	       
      	        assertNotNull(bean.getManufacturer());
      	    }
      	    
      	    
      	    public void testUpdateManufacturer() throws Exception {
      	        //bean.setId(manufId);
      	        //assertEquals(bean.updateManufacturer(), "success");
      	       // assertNotNull(bean.getName());
      	    }
      	    
      	    public void testDeleteManufacturer() throws Exception {
      	    	Manufacturer manuf = new Manufacturer();
      	    	manuf.setId(new Long(manufId));	        
      	        bean.setManufacturer(manuf);
      	        //assertEquals(bean.delete(), "success");
      	    }
      
      }

      Comment


      • #4
        Ok, are you saying you've fixed the problem then? You set the working directory to WebRoot?

        Comment

        Working...
        X