热门文章 | 热门软件| 热门源码 | 热门电影 | 知识库 | 联系我们
软件 源码 教程 影视 健康 招聘
  HTML | JavaScript | ASP | PHP | JSP | NET | VB | VC | VF | Windows | Linux | Mysql | Mssql | Oracle | Struts 
当前位置: 创世纪计算机资源网 -> 文章频道 ->struts 
站内搜索:
利用strutstestcase测试struts应用程序(1)
作者:叶枫 来源:www.matrix.org.cn 整理日期:2007-3-4

版权声明:本文可以自由转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
作者:叶枫(http://www.matrix.org.cn/page/叶枫)
原文:[http://www.matrix.org.cn/resource/article/44/44008_strutstestcase.html]http://www.matrix.org.cn/resource/article/44/44008_strutstestcase.html[/url]
关键字:struts strutstestcase


一、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>

[1]  [2]  
相关文章