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

四、初试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&param2=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文件


七、结束语
j2ee应用程序的测试在开发过程中占有相当重要的位置,利用strutstestcase能极大方便你测试基于struts的应用程序.

关于作者:
叶枫:热爱java和oracle. 在软件开发有近10年, 目前在国外一家美国大公司担任sa, 负责技术研究。作者blog:http://blog.matrix.org.cn/page/叶枫


 

[1]  [2]  
相关文章