2011-10-12 134 views
1

所有资源的路径,我疑惑Struts2的动作单元测试无法获取类路径的资源

import org.apache.struts2.StrutsSpringTestCase; 
import org.junit.Test; 
import com.opensymphony.xwork2.ActionProxy; 

public class TestLoginAction extends StrutsSpringTestCase { 
    @Test 
    public void testCheck() throws Exception { 
     ActionProxy proxy = null; 
     LoginAction test = null; 

     request.setParameter("username", "admin"); 
     proxy = getActionProxy("/checkLogin"); 
     test = (LoginAction) proxy.getAction(); 

     String result = proxy.execute(); 

     assertEquals("error", result); 
     assertEquals("admin", test.getUsername()); 

    } 
} 

它抛出的警告和例外情况:

无法获取资源的路径对于类路径的资源[WEB-INF/JSP /] java.io.FileNotFoundException:类路径资源[WEB-INF/JSP /]不能被解析到URL,因为它不存在

回答

1

抛出异常的原因已经找到, 我用struts-convention找到我的动作类,但是 ,这个配置是jsp文件的基本搜索路径,所以当然约定无法识别java的路径类路径,我会提供两个解决办法在这里:

  1. 可以修改配置值“/ WEB-INF/JSP”现有的类路径,如“com.foo.bar”,使会议顺利解决班级路径

  2. 重写MockServletContext并吞下抛出异常 public Set getResourcePaths(String path){ .... }

+0

如果此问题解决了问题,请使用此问题右侧的复选标记标记为选定答案。 – ahsteele

2

StrutsSpringTestCase期待Spring配置以从类路径被加载:applicationContext.xml中。您可以通过将此行为添加到测试操作中来覆盖此行为:

@Override 
public String getContextLocations() { 
    return "path/to/your/TestLoginAction-context.xml"; 
} 
0

您可以在struts.xml中添加一行以解决该问题。

<constant name="struts.convention.result.path" value="/your/path" />