`
friping
  • 浏览: 130212 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

用StrutsTestCase 测试 struts

阅读更多

  一、Struts测试概述
  
  一个具有良好系统架构的J2EE应用程序至少有三层组成,即表现层,商业层和系统
  
  集成层(包括数据存取以及和其他系统集成),目前,Struts是应用比较广泛,实现MVC2模式应用于表现层的一种技术. 在这里面,Struts Action主要用来完成一些简单的数据校验,转换,以及流程转发控制(注重:这里流程不是业务规则). 因此在对整个应用程序进行测试时,我们同时也要测试Struts Action.
  
  但是,测试Struts Action相对测试简单的JavaBean是比较困难,因为Struts是运行在Web服务器中, 因此要测试Struts Action就必须发布应用程序然后才能测试. 我们想象一下,对于一个拥有上千个jsp page和数百甚至数千Java Classes的大规模应用程序,要把他们发布到诸如Weblogic之类的应用服务器再测试,需要多少的时间和硬件资源? 所以这种模式的测试是非常费时费力的.
  
  所以,假如有一种办法能够不用发布应用程序,不需要Web服务器就能象测试普通Java Class一样测试Struts Action,那就能极大地加强Struts的可测试性能,使应用程序测试更为轻易,简单快速. 现在这个工具来了,这就是StrutsTestCase.
  
  二、StrutsTestCase 概述
  
  StrutsTestCase 是一个功能强大且轻易使用的Struts Action开源测试工具,它本身就是在大名鼎鼎的JUnit基础上发展起来的。因此通过和JUnit结合使用能极大加强应用程序的测试并加快应用程序的开发.
  
  StrutsTestCase提供了两者测试方式,模拟方式和容器测试方式. 所谓模拟方式就是有StrutsTestCase本身来模拟Web服务器. 而容器测试方式则需要Web服务器. 本文要讨论的是前者,原因很简单,不需要Web服务器就能象测试普通的Java Class一样测试Struts Action.
  
  三、预备StrutsTestCase和Struts Action/ActionForm/Config
  
  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.
  
  在本文中,我们用一个简单的例子来做测试. 假设我们有一张表Hotline(country varchar2(50),pno varchar2(50)),
  
  我们要做的是根据输入条件从这张表检索相应的记录.检索条件是country.
  
  Value Object:
  package sample;
  public class HotlineDTO implements Serializable{
  private String country = "";
  private String pno = "";
  /**
  * Method HotlineActionForm
  *
  *
  */
  public HotlineDTO () {
  super();
  }
  public void setCountry(String country) {
  this.country = country;
  }
  public void setPno(String pno) {
  this.pno = pno;
  }
  public String getCountry() {
  return (this.country);
  }
  public String getPno() {
  return (this.pno);
  }
  }
  
  ActionForm:
  package sample;
  import org.apache.struts.action.ActionForm;
  public class HotlineActionForm extends ActionForm{
  private String country = "";
  private String pno = "";
  /**
  * Method HotlineActionForm
  *
  *
  */
  public HotlineActionForm() {
  super();
  }
  public void setCountry(String country) {
  this.country = country;
  }
  public void setPno(String pno) {
  this.pno = pno;
  }
  public String getCountry() {
  return (this.country);
  }
  public String getPno() {
  return (this.pno);
  }
  }
  
  Action Class:
  public class SearchHotlineAction extends Action {
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
  HttpServletResponse response) throws Exception {
  String target = "";
  try{
  //调用HotlineDAO检索hotline
  String country=((HotlineActionForm)form).getCountry();
  List hotlineList = HotlineDAO.getHotlineList(country);
  if(hotlineList!=null && hotlineList.size()>0){
  request.setAttribute("hotlineList",hotlineList);
  target = "hotlineList";
  }else{
  target = "notfound";
  }
  }catch(Exception ex){
  ....
  }
  }
  
  Struts Config:
  <struts-config>
  <form-beans>
  <form-bean name="hotlineActionForm" type="sample.HotlineActionForm" />
  .......  </form-beans> <action-mappings>
  <action path="/SearchHotline"
  name="hotlineActionForm"
  type="sample.SearchHotlineAction "
  scope="request"
  validate="false">
  <forward name="hotlineList" path="/hotlineList.jsp"/>
  <forward name="notfound" path="/searchHotline.jsp"/>
  </action>
  .....
  <action-mappings>
  ........ <struts-config>
  
  四、初试StrutsTestCase
  
  当采用模拟方式时,所有的StrutsTestCase测试Class都是从MockStrutsTestCase继续下来的.
  
  下面我们就创建一个最简单的测试Class.
  public class SearchHotlineAction extends MockStrutsTestCase {
  public void setUp()throws Exception{
  }
  public void tearDown()throws Exception{
  }
  public void testSearchHotline() throws Exception{
  setRequestPathInfo("/SearchHotline.do");
  addRequestParameter("country", "CN");
  actionPerform();
  }
  }
  
  上面的Class相信用过JUnit的朋友都很熟悉.
  
  好了,一个简单的测试例子就完成了,假如你用的是Eclipse就选择Run-Run...-JUnit-New就可以直接运行.不需要发布你的程序,不需要任何的Web服务器支持,就可以测试Struts Action,这就是StrutsTestCase带来的好处.下面简单地介绍一下它是怎么工作的.
  
  在上面的例子中,我们调用setRequestPathInfo()告诉StrutsTestCase我们要模拟JSP调用SearchHotline.do这个Action,并且调用addRequestParameter()增加了一个参数country.最后调用actionPerform()运行.
  
  看到这里,大家发现一个问题没有? 在上面Action的源代码里我们是通过
  
  String country=((HotlineActionForm)form).getCountry();
  
  也就是ActionForm来取得输入的参数值,可我们在testSearchHotline()方法里并没有设置ActionForm?
  
  那么它是怎么出来的呢? 其实大家假如熟悉Struts的运行流程的话就知道,JSP接受用户的输入并发请求时都是类似这样的http://hostname/servletName?param1=value1¶m2=value2. 只是Struts接受到这些参数后再根据Struts Config里的Action和ActionForm的映射把他们转为ActionForm后传给Action的.
  
  在上面的例子,我们只是简单地运行了Action,那么Action是否正确执行以及返回的结果是不是我们想要的呢?
  
  我们继续完善一下testSearchHotline()这个Method.
  
  public void testSearchHotline() throws Exception{
  setRequestPathInfo("/SearchHotline.do");
  addRequestParameter("country", "CN");
  actionPerform();
  verifyNoActionErrors();
  verifyForward("hotlineList");
  assertNotNull(request.getAttribute("hotlineList"));
  List hotlineList = (List) request.getAttribute("hotlineList");
  for (Iterator it = hotlineList.iterator();it.hasNext();){
  ....
  }
  }
  
  我们在actionPerform()后增加了几行语句来断定Struts Action是否正确执行.
  
  verifyNoActionErrors() -- 判定Action里没有任何的Action;
  verifyForward("hotlineList") -- 判定Action确实转发到hotlineList;
  assertNotNull(request.getAttribute("hotlineList")) -- 判定Action确实返回了hotlineList并且不为空
  
  到这里,我们已经基本上讨论完了StrutsTestCase的核心部分. 从头到尾,我们没有发布应用程序,也不需要Web服务器,对我们来讲,Struts Action就象普通的Java Class一样轻易调试测试.这就是StrutsTestCase给我们带来的方便.
  
  五、深入StrutsTestCase
  
  除了以上我们用到的几个断定和校验方法外,StrutsTestCase还提供了其他几个方法便于我们测试Struts Action. 下面我一一讲述,具体的大家可以参考文档.
  
  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属性
  verifyTilesForward/verifyInputTilesForward--和以上类似,应用程序使用到tiles时用的
  
  六、关于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:/app/web/WEB-INF/web.xml(struts-config.xml)的话,就需要把d:/app/web加到classpath.
  
  2. 假如你的struts config是strust-config-module.xml,那么必须调用setConfigFile()设置你的struts config文件
  
  

分享到:
评论

相关推荐

    精通Struts基于MVC的Java Web设计与开发 孙卫琴 光盘

    内容推荐 Struts是目前非常流行的基于MVC的Java ...第19章到第21章介绍了如何采用第三方软件,如Apache Common Logging API、Log4J、ANT和StrutsTestCase,来控制Struts应用的输出日志、管理以及测试Struts应用项目。

    追求代码质量:测试Struts遗留的应用程序

    用DbUnit进行测试反复测试StrutsStruts的集成测试虽然Struts正在慢慢退出Web框架的历史舞台,但它的遗产仍然存在,存在的形式主要是需要测试和维护的应用程序。这个月,AndrewGlover向您介绍如何使用JUnit的...

    精通 Struts:基于 MVC 的 JavaWeb 设计与开发(PDF)

    Struts是目前非常流行的基于MVC的Java Web框架。...第19章到第21章介绍了如何采用第三方软件,如Apache Common Logging API、Log4J、ANT和StrutsTestCase,来控制Struts应用的输出日志、管理以及测试Struts应用项目。

    精通Struts_基于MVC的Java Web设计与开发

    Struts是目前非常流行的基于MVC的Java Web框架。...第19章到第21章介绍了如何采用第三方软件,如Apache Common Logging API、Log4J、ANT和StrutsTestCase,来控制Struts应用的输出日志、管理以及测试Struts应用项目。

    精通struts:基于mvc的java web设计与开发part3

    Struts是目前非常流行的基于MVC的Java Web框架。...第19章到第21章介绍了如何采用第三方软件,如Apache Common Logging API、Log4J、ANT和StrutsTestCase,来控制Struts应用的输出日志、管理以及测试Struts应用项目。

    精通struts:基于mvc的java web设计与开发part2

    Struts是目前非常流行的基于MVC的Java Web框架。...第19章到第21章介绍了如何采用第三方软件,如Apache Common Logging API、Log4J、ANT和StrutsTestCase,来控制Struts应用的输出日志、管理以及测试Struts应用项目。

    精通struts:基于mvc的java web设计与开发part1

    第19章到第21章介绍了如何采用第三方软件,如Apache Common Logging API、Log4J、ANT和StrutsTestCase,来控制Struts应用的输出日志、管理以及测试Struts应用项目。 这个真的是包含所有的源码。。。

    struts+hibernate课件

    b、ActionForms使用不便、无法进行单元测试(StrutsTestCase只能用于集成) Struts提供了几个标签库?都是什么标签库? Struts提供了五个标签库,即:HTML、Bean、Logic、Template和Nested。 HTML 标签 用来创建能够...

    struts2-junit-plugin-2.1.6.jar

    struts2-junit-plugin-2.1.6.jar struts2测试 action测试 strutstestcase

    基于MVC的java Web设计与开发

    Struts是目前非常流行的基于MVC的Java Web框架。...第19章到第21章介绍了如何采用第三方软件,如Apache Common Logging API、Log4J、ANT和StrutsTestCase,来控制Struts应用的输出日志、管理以及测试Struts应用项目。

    从Java走向Java+EE+.rar

    第1章 Java EE的基本知识 1 1.1 Java EE的出现及其特点 1 1.2 Java EE的分层模型和...23.4 利用StrutsTestCase对Struts进行测试 328 23.5 压力测试和JMeter 334 23.6 其他开源测试工具 339 23.7 小结 343

Global site tag (gtag.js) - Google Analytics