Struts 1.2 如何测试Action

系统 1720 0

在测试驱动开发(Test-Driven Development TDD)的理念下,单元测试技术显得至关重要。

在基于Struts项目中,会有大量封装业务流程的Action类,对它们的测试是必要的。但由于Struts1中Action与Servlet容器的耦合程度较深,对Action的测试比较困难(Action的execute方法需要HttpServletRequest和HttpServletResponse对象才能调用)显然不适合大规模,迭代式的测试。

 <wbr></wbr> <wbr></wbr> <wbr></wbr> 对于在测试过程中,对于某些不易构建的对象(如HTTPServletRequest对象),可以创建一个虚拟的对象以便进行测试,这样的对象称之为mock对象,mock这个词的含义是“模仿,仿制品”。

 <wbr></wbr> <wbr></wbr> <wbr></wbr> StrutTest组件作为Junit框架的扩展可以为我们创建出用于测试Strut Action的mock对象。

 <wbr></wbr> <wbr></wbr> <wbr></wbr> 可以从其官方网站下载该组件 http://strutstestcase.sourceforge.net

 <wbr></wbr> <wbr></wbr> <wbr></wbr> 下面的Demo用于演示使用strutstest组件对Struts Action进行基于Junit的单元测试。

 <wbr></wbr> <wbr></wbr> <wbr></wbr> 以下是一个很简单的Action及其配置文件,是我们的测试目标,这个Action实现了一个简单的登登~~登录功能(这样的Demo太多了是吧,我尽量保证是最后一次登录

 <wbr></wbr>

 <wbr></wbr>public class LogonAction extends Action {

 <wbr></wbr> <wbr></wbr> public ActionForward execute(ActionMapping mapping, ActionForm form,
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> HttpServletRequest request, HttpServletResponse response)
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> throws Exception {
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> LogonForm f = (LogonForm) form;
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> String username = f.getUsername();
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> String password = f.getPassword();
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> if ("whatisjava".equals(username) && "java".equals(password)) {
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> HttpSession session = request.getSession();
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> session.setAttribute("username", username);
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> return mapping.findForward("success");
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> } else {
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> ActionMessages messages = new ActionMessages();
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr>messages.add("fail.logon", new ActionMessage("msg.fail.logon"));
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> saveMessages(request, messages);
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> return mapping.findForward("failed");
 <wbr></wbr> <wbr></wbr> <wbr></wbr>  <wbr></wbr> <wbr></wbr> }
 <wbr></wbr> <wbr></wbr> <wbr></wbr>}

}

 <wbr></wbr>

<action path="/logon" type="action.LogonAction" name="logonForm">
 <wbr></wbr> <wbr></wbr> <wbr></wbr><forward name="success" path="/jsp/success.jsp"/>
 <wbr></wbr> <wbr></wbr> <wbr></wbr><forward name="failed" path="/jsp/logon_form.jsp"/>
 <wbr></wbr></action>

 <wbr></wbr>

 <wbr></wbr> <wbr></wbr> <wbr></wbr> 我们要测试的目标是,登录成功或失败后是否返回合适的forward对象,而且登录成功后session对象中是否存有用户名,而登录失败后request中是存有相应的ActionMessage对象。

 <wbr></wbr> <wbr></wbr> <wbr></wbr> 首先将下载到的StrutsTest发行包中的strutstest-2.1.4.jar文件复制到应用的/WEB-INF/lib目录下,另外该组件的运行还需要apache的commons-collections-2.1.jar组件。

 <wbr></wbr> <wbr></wbr> <wbr></wbr> 编写单元测试类TestLogonAction如下,注意:该测试类要继承StrutsTest的MockStrutsTestCase类。根据测试用例编写测试方法:testSuccess用于测试登录成功的情况而testFailed用于测试登录失败的情形。 MockStrutsTestCase几个重要的API需要了解一下:

  • setContextDirectory(File) 用于指定web应用的根目录,StrutsTest在工作的时候需要读取Struts配置文件struts-config.xml,StrutsTest会在指定的根目录下的WEB-INF目录下查找struts-config.xml文件。注意,如果使用MyEclipse,它创建Web项目时会默认在工程目录下建立一个名为WebRoot的子目录作为应用的根目录,这样就需要运行setContextDirectory(new File("WebRoot"))方法。
  • addRequestParameter(String, String) 该方法用于向StrutsTest提供的mock对象中添加请求参数对以模拟用户提交的请求信息,这里使用该方法添加username和password的请求参数对用于模拟用户的登录表单提交。
  • setRequestPathInfo(String) 该方法用于设定要访问的Action,String类型的参数为要访问的Action在Struts配置文件中对应的path属性值。
  • getSession() 用于获取StrutsTest创建的用于模拟HttpSesssion对象的mock对象。
  • getRequest() 用于获取StrutsTest创建的用于模拟HttpServletRequest对象的mock对象。
  • verifyForward(String) 断言方法,表示Action返回的特定的Forward对象,其中字符串参数表示应该返回的Forward对象在Struts配置文件中对于的forward元素的name属性。例如verifyForward("success"); 如果返回的ActionForward对象对应的name不是success,则断言失败。 verifyForwardPath(String)类似于verifyForward方法,只不过String参数表示的是Action返回的forward对象所对应的URI地址。
  • verifyActionMessages(String[]) 断言方法,表示request中有一系列特定property名称的ActionMessage对象。
  • actionPerform() 执行测试过程。

 <wbr></wbr> <wbr></wbr> <wbr></wbr> 测试类代码如下: <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr>

public class TestLogonAction extends MockStrutsTestCase {
 <wbr></wbr> <wbr></wbr> protected void setUp() throws Exception {
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> super.setUp();
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> setContextDirectory(new File("WebRoot"));
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> setRequestPathInfo("/logon");
 <wbr></wbr>  <wbr></wbr>}

 <wbr></wbr> <wbr></wbr> public void testSuccess() throws Exception {
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> addRequestParameter("username", "whatisjava");
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> addRequestParameter("password", "java");
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> actionPerform();
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> verifyForward("success");
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr>  <wbr></wbr>verifyForwardPath("/jsp/success.jsp");
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr>  <wbr></wbr>assertEquals("whatisjava", getSession().getAttribute("username"));
 <wbr></wbr> <wbr></wbr> }

 <wbr></wbr> <wbr></wbr> public void testFailed() {
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> addRequestParameter("username", "whatisjava");
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> addRequestParameter("password", "avaj");
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> actionPerform();
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> verifyForward("failed");
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> verifyForwardPath("/jsp/logon_form.jsp");
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> verifyActionMessages(new String[]{"msg.fail.logon"});
 <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> <wbr></wbr> assertNull(getSession().getAttribute("username"));

 <wbr></wbr> <wbr></wbr> }

}

Struts 1.2 如何测试Action


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论