Struts测试之action测试

来源:百度文库 编辑:神马文学网 时间:2024/04/29 09:07:18
Struts 测试之 action 测试
测试 Struts Action 相对比较困难 , 因为 Struts 是运行在 Web 服务器中 , 因此要测试 Struts Action 就必须发布应用程序然后才能测试,换言之,我们必须要有 Web 容器的支持 . 我们想象一下 , 对于一个拥有上千个 JSP page 和数百甚至数千 Java Classes 的大规模应用程序 , 要把他们发布到诸如Weblogic 之类的应用服务器再测试 , 需要多少的时间和硬件资源 ? 所以这种模式的测试是非常费时费力的 .
所以 , 如果有一种办法能够不用发布应用程序 , 不需要 Web 服务器就能象测试普通 Java Class 一样测试 Struts Action, 那就能极大地加强 Struts 的可测试性能 , 使应用程序测试更为容易 , 简单快速 . 现在这个工具来了 , 这就是 StrutsTestCase.
StrutsTestCase 是一个开源工具 , 可以到 http://strutstestcase.sourceforge.net 下载 . 目前最新版本是 2.1.3, 如果你使用 Servlet2.3 就下载 StrutsTestCase213-2.3.jar, 使用 Servlet2.4 的就下载 StrutsTestCase213-2.4.jar. 另外 StrutsTestCase 本身就是从 JUnit 继承的 , 所以你还需要下载 JUnit3.8.1.
下面就以一个简单的 LogonAction 为例测试一下:
public class LogonAction extends Action {
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaValidatorForm dynaForm = (DynaValidatorForm)form;
String name = (String)dynaForm.get("username");
String password = (String)dynaForm.get("password");
if (name.equals("wangxq") && password.equals("wangxq")){
request.setAttribute("valid_user",form);
return mapping.findForward("admin");
}
return mapping.findForward("success");
}
}
LogonAction 的简单说明:从 Logon 的页面中输入用户名和密码,在 LogonAction 中作判断,并且作相应的跳转。
对其的测试代码如下:
public class LogonActionTest extends MockStrutsTestCase {
protected void setUp() throws Exception {
super.setUp();
setContextDirectory(new File("WebRoot")); // 设置 WEB-INF 的上级目录,让程序可以找到 struts-config.xml 文件
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testNoParameters(){
setRequestPathInfo("/logon");
actionPerform();
verifyInputForward();
String[] actionErrors = {"errors.required","errors.required"};
verifyActionErrors(actionErrors);
verifyInputForward();
}
public void testOneParameters(){
setRequestPathInfo("/logon");
addRequestParameter("username","wangxq");
actionPerform();
// 校验 Action 是否转发到 Action Mapping 里的 input 属性
verifyInputForward();
String[] actionErrors ={"errors.required"};
verifyActionErrors(actionErrors);
verifyInputForward();
}
public void testSuccessAdmin(){
// 设置 Request 的请求,说明该 Request 请求的是哪一个 Action ,或者说,请求的是哪一个 .do 文件。
setRequestPathInfo("/logon");
// 将参数和其对应的值加入到 request 中,相当于是 action 对应的 formbean 传过来的值,即用户在登陆界面输入的值。
addRequestParameter("username","wangxq");
addRequestParameter("password","wangxq");
// 执行这个请求,即执行 action 中对应的 execute 方法。
actionPerform();
// 验证 forward 的名字是否正确,即有没有跳转到预期的页面。
verifyForward("admin");
// 验证没有任何的 ActionErrors 。
verifyNoActionErrors();
}
public void testSuccessLogon(){
setRequestPathInfo("/logon");
addRequestParameter("username","aaaaaa");
addRequestParameter("password","bbbbbb");
actionPerform();
verifyForward("success");
verifyNoActionErrors();
}
}
补充说明其中的一些方法:
verifyActionErrors/Messages -- 校验 ActionActionServlet controller 是否发送了 ActionError 或 ActionMessage. 参数为 ActionError/Message Key
verifyNoActionErrors/Messages -- 校验 ActionActionServlet controller 没有发送 ActionError 或 ActionMessage
VerifyForward -- 校验 Action 是否正确转发到指定的 ActionForward.
VerifyForwardPath -- 校验 Action 是否正确转发到指定的 URL
verifyInputForward -- 校验 Action 是否转发到 Action Mapping 里的 input 属性
其他的方法可以参考具体的文档说明。
还有一点需要说明:
关于Web.xml和Struts-Config.xml
缺省情况下 ,StrutsTestCase 认为你的 Web.xml 和 struts-config.xml 的路径分别是 :
/WEB-INF/web.xml 和 /WEB-INF/struts-config.xml
1. 假如你的 web.xml/struts-config.xml 的路径是
d:/application/web/WEB-INF/web.xml(struts-config.xml) 的话 , 就需要把 d:/ application /web 加到 classpath.
或者更简单的方法是 setContextDirectory(new File("web")) 这样就可以找到了。
2. 假如你的 struts config 是 strust-config-module.xml,
那么必须调用 setConfigFile() 设置你的 struts config 文件
深入使用:
input="/handle.do?method=setUp"
name=" handleForm"
type=" handleAction"
scope="session"
parameter="method"
validate="true">


这段配置文件中,使用了 parameter="method" 的配置,这样在测试的时候就需要设置以下:
测试代码中应该加入:
addRequestParameter("method ","setUp");
这样,在执行 actionPerform() 时,程序就自动进入 setUp 的方法,执行该方法的测试。