Menu

Received an exception while loading web.xml

Ram
2008-05-21
2013-04-25
  • Ram

    Ram - 2008-05-21

    package au.com.hubbub.consol.web.orderattribute;

    import java.io.File;

    import servletunit.struts.MockStrutsTestCase;

    public class ManageAttributeTypeActionTest extends MockStrutsTestCase{
       
        public void setUp() throws Exception{
           
            super.setUp();
            if(new File("war/src/main/webapp/WEB-INF/web.xml").exists()){
                System.out.println("FILE FOUND");
            }
            this.setContextDirectory(new File("war/src/main/webapp/"));
            this.setServletConfigFile("war/src/main/webapp/WEB-INF/web.xml");
            this.setConfigFile("war/src/main/webapp/WEB-INF/mappings/orderattribute-mappings.xml");
        }
       
        public void tearDown() throws Exception{
            super.tearDown();
        }
       
        public ManageAttributeTypeActionTest(String testName){
            super(testName);
        }
       
        public void testManageAttributePath(){    
            setRequestPathInfo("/orderattribute/manageattributetypes.do");
            verifyForward("success");
            actionPerform();
        }   

    }

    When I run the above test I get an exception "junit.framework.AssertionFailedError: Received an exception while loading web.xml". But the above piece of code does locate the web.xml file. Could some one tell me how do i rectify this problem.

    The stack trace is like below

    junit.framework.AssertionFailedError: Received an exception while loading web.xml - class java.lang.NullPointerException : null
        at servletunit.struts.MockStrutsTestCase.setServletConfigFile(MockStrutsTestCase.java:535)
        at au.com.hubbub.consol.web.orderattribute.ManageAttributeTypeActionTest.setUp(ManageAttributeTypeActionTest.java:16)
        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.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
        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)

     
    • Patrick

      Patrick - 2008-05-21

      Are you unit testing the source or the compiled code?

      Here's and example of how I've been doing it...

          public void setUp() throws Exception {
              super.setUp();
          }
          public void testForwardInternalUserToUserSwitch() throws Exception{
              setContextDirectory(new File("webapp.war"));
              setConfigFile("accountmanagement","/WEB-INF/struts-accountmanagement-config.xml");
              setRequestPathInfo("/accountmanagement","/ReportSettingsLoad");
              actionPerform();
              verifyForwardPath("/merchantaccount/MerchantAccountFunctionalUserSwitchLoad.do");
          }

       
      • Ram

        Ram - 2008-05-21

        Yeah i am unit testing my code. But where is your webapp.war located inside the project.

        Cheers,
        Ram

         
        • Patrick

          Patrick - 2008-05-22

          Where are you writing your compiled code? The root directory of your compiled code would be what I called "webapp.war" so after compiling I would have something like this.

          /webapp.war     This is the root directory of the web application. All JSP and XHTML files are stored here.

          /webapp.war/WEB-INF     This directory contains all resources related to the application that are not in the document root of the application. This is where your web application deployment descriptor is located. Note that the WEB-INF directory is not part of the public document. No files contained in this directory can be served directly to a client.

          /webapp.war/WEB-INF/classes     This directory is where servlet and utility classes are located.

          /webapp.war/WEB-INF/lib     This directory contains Java Archive files that the web application depends upon. For example, this is where you would place a JAR file that contained a JDBC driver.

          Copied from http://www.onjava.com/pub/a/onjava/2001/03/15/tomcat.html

           
  • Usman Chaudhri

    Usman Chaudhri - 2010-03-30

    Once you setContextDirectory(), passing in the absolute path of your context directory - fully qualified file system path of your application - you can than invoke setConfigFile() using the relative path.

    For Example:
    Your application name is "space" and it resides under c:\space.

    Your file structure:
    Main application folder    c:\space
    web.xml path -                   c:\space\ WEB-INF\web.xml
    Action controller path -     c:\space\ WEB-INF\struts-config.xml
                                    
    Here's how you would load web.xml in struts test case
    setContextDirectory(new File("c:\space"));
    setConfigFile("/WEB-INF/web.xml");
    setConfigFile("/WEB-INF/struts-config.xml");

    T

     
  • Preethi Reddy

    Preethi Reddy - 2010-05-11

    Did anybody solve this problem?
    I am getting the same error.
    I can read the file (web.xml) with a regular reader but there is NPE when the digester parses the input.

    Any help is greatly appreciated.

     

Log in to post a comment.