-->

StrutsTestCase The /WEB-INF/web.xml was not found

2019-05-21 18:17发布

问题:

I use StrutsTestCase2.4 under netbeans7.0 with struts1.3
When running testCase, it shows:

Error initializing action servlet
javax.servlet.UnavailableException: The /WEB-INF/web.xml was not found.

I've googled this problem and it is suggested to be solved by setContextDirectory(new File("../web"));:

protected void setUp() throws Exception
{
    super.setUp();
    setContextDirectory(new File("../web"));
 }

But I am not quite sure what the location of new File() should be.
My File tree is

|───build
│   ├───test
│   │   └───classes
│   │       └───com
│   │           └───stolon
│   │               ├───common
│   │               ├───database
│   │               ├───helpers
│   │               └───struts
│   └───web
│       ├───META-INF
│       └───WEB-INF
│           ├───classes
│           │   └───com
│           │       └───stolon
│           │           ├───algorithm
│           │           ├───database
│           │           ├───helpers
│           │           ├───servlet
│           │           ├───structures
│           │           └───struts
│           └───lib
├───nbproject
│   └───private
├───src
│   ├───conf
│   └───java
│       └───com
│           └───stolon
│               ├───algorithm
│               ├───database
│               ├───helpers
│               ├───servlet
│               ├───structures
│               └───struts
├───test
│   └───com
│       └───stolon
│           ├───common
│           ├───database
│           ├───helpers
│           └───struts
└───web
    ├───META-INF
    └───WEB-INF

My test file is under test-com-stolon-struts.

回答1:

I just ran in to this. WEB-INF/web.xml (and probably struts-config.xml, etc.) must be on your classpath when the tests are running. Make sure netbeans is putting /build/web/ on the test classpath.

If you were using maven, you would add WEB-INF/*.xml as a test resource.

    <testResources>
        <testResource>
            <directory>WEB-INF</directory>
            <targetPath>/WEB-INF</targetPath>
            <includes>
                <include>*.xml</include>
            </includes>
        </testResource>
    </testResources>


回答2:

From the directory structure, it seems : setContextDirectory(new File("../../../../web"));



回答3:

So, based on the tree above, the location of new File() should be "web": protected void setUp() throws Exception { super.setUp(); setContextDirectory(new File("web")); }