Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到web.xml。模拟struts测试用例_Java_Junit_Mocking_Struts_Web.xml - Fatal编程技术网

Java 找不到web.xml。模拟struts测试用例

Java 找不到web.xml。模拟struts测试用例,java,junit,mocking,struts,web.xml,Java,Junit,Mocking,Struts,Web.xml,这是我的项目目录结构: TOPS-WEB |___ | Java Resources |___ WebContent |____ WEB-INF |____ | web.xml |_

这是我的项目目录结构:

TOPS-WEB
      |___
      |    Java Resources
      |___ 
           WebContent
                 |____
                      WEB-INF
                            |____
                            |    web.xml
                            |____  
                            |    lib
                            |       |__
                            |          strutstest-2.1.4.jar
                            |____
                                 struts
                                      |__
                                         struts-info-config.xml 
当我运行测试时,我得到以下错误。此错误由行引发 testInfo()方法中的setServletConfig(…)

我的测试班

public class MyTest extends MockStrutsTestCase{

    @Override
    protected void setUp() throws Exception {
        super.setUp();           
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public MyTest(String testName){
        super(testName);
    }

    public void testInfo(){         
        setContextDirectory(new File("TOPS-WEB/WebContent"));       
        setConfigFile(this.getSession().getServletContext().getRealPath("/WEB-INF/struts/struts-info-config.xml"));         
        *setServletConfigFile(this.getSession().getServletContext().getRealPath("/WEB-INF/web.xml"));*
        setRequestPathInfo("/infoPopup");
        actionPerform();            
        }    
    }

我看到一些地方提到你必须使用maven,但我不是。我只需右键单击测试类并将其作为junit运行。这可能是原因吗

您的问题是将上下文目录(使用
setContextDirectory
)设置为不存在的路径

新文件(字符串)
,当
字符串
是相对路径名时,将解析为相对于当前工作目录的路径名。然后,假设当前工作目录是Tomcat放置其分解的WAR文件的父目录

打印此文件:

System.out.println(System.getProperty(“user.dir”))

您将获得进程的当前工作目录。然后,相应地调整
setContextDirectory

是否执行
新文件(“TOPS-WEB/WebContent”).exists()
返回
true
?可能是工作目录不正确。我没有找到System.out.println(新文件(“TOPS-WEB/WebContent”).exists();结果是假的。
public class MyTest extends MockStrutsTestCase{

    @Override
    protected void setUp() throws Exception {
        super.setUp();           
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public MyTest(String testName){
        super(testName);
    }

    public void testInfo(){         
        setContextDirectory(new File("TOPS-WEB/WebContent"));       
        setConfigFile(this.getSession().getServletContext().getRealPath("/WEB-INF/struts/struts-info-config.xml"));         
        *setServletConfigFile(this.getSession().getServletContext().getRealPath("/WEB-INF/web.xml"));*
        setRequestPathInfo("/infoPopup");
        actionPerform();            
        }    
    }