测试 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 {
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 文件
深入使用:
<action path=”/handle”
input=”/handle.do?method=setUp”
name=” handleForm”
type=” handleAction”
scope=”session”
parameter=”method”
validate=”true”>
这段配置文件中,使用了 parameter=”method” 的配置,这样在测试的时候就需要设置以下:
测试代码中应该加入:
addRequestParameter(“method “,”setUp”);
这样,在执行 actionPerform() 时,程序就自动进入 setUp 的方法,执行该方法的测试。
所以 , 如果有一种办法能够不用发布应用程序 , 不需要 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 {
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 文件
深入使用:
<action path=”/handle”
input=”/handle.do?method=setUp”
name=” handleForm”
type=” handleAction”
scope=”session”
parameter=”method”
validate=”true”>
这段配置文件中,使用了 parameter=”method” 的配置,这样在测试的时候就需要设置以下:
测试代码中应该加入:
addRequestParameter(“method “,”setUp”);
这样,在执行 actionPerform() 时,程序就自动进入 setUp 的方法,执行该方法的测试。
另外,也可以用EasyMock来进行Action测试
------------------------------------------------------------------------------------------------------------
简单的说吧,设你用的是JUNIT4
新建一个工程,并让它引用你的原工程
在新工程里建立一个普通的类
在类里建一个普通的方法,但方法一定要用@test标记
然后就可以运行这个测试了,如果函数的返回值确实与你的想定相同,则测试通过,否则NG
好吧,你的题目是要ACTION,那我们试着写一个
import static org.junit.Assert.*; // 这个很容易被忘的,要注意/** * JUNIT测试类,基于JUNIT4 */
public class TestAction{
@Test // 测试方法前必须加这个东西
public void testLogin(){
// 准备测试,生成被测试的类的实例
LoginAction action = new LoginAction();
// 准备第一套测试数据,一个合法的用户
action.setUsername( "admin" );
action.setPassword( "1234" );
// 用第一套数据测试login方法
String rtn = action.login();
// 合法用户的返回值应该是success
assertEquals( rtn, "success" );
// 准备第二套数据,一个非法用户
action.setUserPwd( "NoUser" );
action.setUserPwd( "1234" );
rtn = action.login();
// 非法用户返回login
assertEquals( rtn, "login" );
}
}