王朝网络
分享
 
 
 

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

王朝java/jsp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

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");

theRequest.addParameter("ServerID", "AA");

theRequest.addParameter("BHD0001", "1");

theRequest.addParameter("E", "0");

}

public final void testDoGetHttpServletRequestHttpServletResponseA() {

try {

wamServlet.doGet(request,response);

} catch (ServletException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//TODO Implement doGet().

}

public final void endDoGetHttpServletRequestHttpServletResponseA(WebResponse theResponse){

System.out.print(theResponse.getText());

}

public void beginDoGetHttpServletRequestHttpServletResponseB(WebRequest theRequest){

theRequest.addParameter("B", "9914");

}

public final void testDoGetHttpServletRequestHttpServletResponseB() {

try {

wamServlet.doGet(request,response);

} catch (ServletException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//TODO Implement doGet().

}

public final void endDoGetHttpServletRequestHttpServletResponseB(WebResponse theResponse){

System.out.print(theResponse.getText());

}

}

详细的api请参照相关的文档,在文件目录下均存在

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有