服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java单体测试工具cactus使用指南


  1 cactus测试的原理
看下面这张图,这是一个测试工具cactus测试servlet的过程,而在过去测试servlet是很麻烦的。
1.首先,junit测试器执行yyytestcase.runtest(),这个函数找到beginxxx(webrequest)执行它,这个过程发生在客户端。webrequest作为参数,它包含了请求的http头、http参数,它将被传送到第2步的重定向代理器中。
2.yyytestcase.runtest()建立一个到重定向代理器的http链接. 上一步的webrequest传了过去。
3.从客户端来看,重定向代理器好像是在服务器运行一样。这意味着你的测试用例类将被实例两次:一次是在客户端(被junit测试器实例),另一次是在服务器端(被重定向代理器实例)。客户端的实例用来执行beginxxx()和endxxx()函数,服务器端实例用来执行第4步中的testxxx()函数。
4.setup(), testxxx()和teardown()被依次执行,它们被重定向代理器以reflection机制执行。当然,setup()和teardown()是可选的(就像在junit里一样)。
5.你的testxxx()将调用服务器端代码测试,并使用junit的断言api来观察执行效果(如assert(), assertequals(), fail(), ...)
6.如果测试失败,你的testxxx()函数抛出的异常将被重定向代理器捕获。
7.如果出现异常,重定向代理器返回它的信息(包含名字,类,栈顶数据)到客户端,然后异常信息将显示在junit的测试器上。
8.如果没有异常产生,yyytestcase.runtest()函数则找到endxxx(org.apache.cactus.webresponse)或endxxx(com.meterware.httpunit.webresponse) 函数执行。在全过程中,你有机会在endxxx()函数检查http头,cookies以及servlet的输出流。

2.cactus 的安装
文件下载地址:

http://www.javaresearch.org/oss/download/cactus/jakarta-cactus-12-1.6.1.zip

使用方式:

解压压缩包后将lib文件夹下的全部jar添加到相关项目目录(为保证确认,可以将路径同时添加到classpath中),配置cactus.properties文件,修改cactus.contexturl值为所在webapp的起始路径,同时将该文件放置到classpath路径中

如例:

# configuration file for cactus.



# each project using cactus need to have such a file put in the client side

# classpath (meaning the directory containgin this file should be in the client

# side classpath, not the file itself of course ... :) )



# defines the urls that will be used by cactus to call it's redirectors

# (servlet and jsp). you need to specify in these urls the webapp context

# that you use for your application. in the example below, the context is

# "test".



cactus.contexturl = http://localhost:8080/fund_cafe

cactus.servletredirectorname = servletredirector

cactus.enablelogging=true

修改web应用下web.xml文件添加相应的servlet映射

如例;

<?xml version="1.0" encoding="utf-8"?>

<!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<servlet>

<servlet-name>ts</servlet-name>

<servlet-class>jp.co.abic.wam.startmenu.forwardlauncher</servlet-class>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>wam_home</param-name>

<param-value>d:/fund_cafe/conf</param-value>

</init-param>

<init-param>

<param-name>forward_servlet</param-name>

<param-value>ts2</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet>

<servlet-name>ts2</servlet-name>

<servlet-class>jp.co.abic.wam.wamservlet</servlet-class>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>wam_home</param-name>

<param-value>d:/fund_cafe/conf</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet>

<servlet-name>servletredirector</servlet-name>

<servlet-class>org.apache.cactus.server.servlettestredirector</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>servletredirector</servlet-name>

<url-pattern>/servletredirector</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>ts</servlet-name>

<url-pattern>/ts</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>ts2</servlet-name>

<url-pattern>/ts2</url-pattern>

</servlet-mapping>

<resource-ref>

<description>oracle datasource example</description>

<res-ref-name>oraclepool</res-ref-name>

<res-type>javax.sql.datasource</res-type>

<res-auth>container</res-auth>

</resource-ref>

</web-app>

3.实际举例
被测试类: forwardlauncher

测试类: test

测试条件1:b=9914,s=123456789,serverid=aa, bhd0001=1,e=0(程序正常路径,显示选择菜单)

测试条件2:b=9914(程序异常路径,显示发生错误的页面)

test类内容如下:

package jp.co.abic.wam;



import java.io.ioexception;



import javax.servlet.servletexception;



import jp.co.abic.wam.startmenu.forwardlauncher;



import org.apache.cactus.servlettestcase;

import org.apache.cactus.webrequest;

import org.apache.cactus.webresponse;



/**

* @author sfluo

*

* todo to change the template for this generated type comment go to

* window - preferences - java - code style - code templates

*/

public class test extends servlettestcase {





forwardlauncher wamservlet=new forwardlauncher();

public static void main(string[] args) {

junit.textui.testrunner.run(test.class);

}



/*

* @see testcase#setup()

*/

protected void setup() throws exception {



config.setinitparameter("forward_servlet","ts2");

wamservlet.init(config);

}



/*

* @see testcase#teardown()

*/

protected void teardown() throws exception {

super.teardown();

}



/*

* class under test for void doget(httpservletrequest, httpservletresponse)

*/

public void begindogethttpservletrequesthttpservletresponsea(webrequest therequest){

therequest.addparameter("b", "9914");

therequest.addparameter("s", "123456789");

扫描关注微信公众号