您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > Cactus
Java單元測試工具cactus使用指南
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/1/22 15:28:24 ] 推薦標(biāo)簽:

1.首先,JUnit測試器執(zhí)行YYYTestCase.runTest(),這個函數(shù)找到beginXXX(WebRequest)執(zhí)行它,這個過程發(fā)生在客戶端。WebRequest作為參數(shù),它包含了請求的HTTP頭、HTTP參數(shù),它將被傳送到第2步的重定向代理器中。
2.YYYTestCase.runTest()建立一個到重定向代理器的HTTP鏈接. 上一步的WebRequest傳了過去。
3.從客戶端來看,重定向代理器好像是在服務(wù)器運(yùn)行一樣。這意味著你的測試用例類將被實(shí)例兩次:一次是在客戶端(被JUnit測試器實(shí)例),另一次是在服務(wù)器端(被重定向代理器實(shí)例)?蛻舳说膶(shí)例用來執(zhí)行beginXXX()和endXXX()函數(shù),服務(wù)器端實(shí)例用來執(zhí)行第4步中的testXXX()函數(shù)。
4.setUp(), testXXX()和tearDown()被依次執(zhí)行,它們被重定向代理器以reflection機(jī)制執(zhí)行。當(dāng)然,setUp()和tearDown()是可選的(像在JUnit里一樣)。
5.你的testXXX()將調(diào)用服務(wù)器端代碼測試,并使用JUnit的斷言API來觀察執(zhí)行效果(如assert(), assertEquals(), fail(), ...)
6.如果測試失敗,你的testXXX()函數(shù)拋出的異常將被重定向代理器捕獲。
7.如果出現(xiàn)異常,重定向代理器返回它的信息(包含名字,類,棧頂數(shù)據(jù))到客戶端,然后異常信息將顯示在JUnit的測試器上。
8.如果沒有異常產(chǎn)生,YYYTestCase.runTest()函數(shù)則找到endXXX(org.apache.cactus.WebResponse)或endXXX(com.meterware.httpunit.WebResponse) 函數(shù)執(zhí)行。在全過程中,你有機(jī)會在endXXX()函數(shù)檢查HTTP頭,Cookies以及servlet的輸出流。

2.cactus 的安裝
文件下載地址:

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

使用方式:

解壓壓縮包后將lib文件夾下的全部JAR添加到相關(guān)項(xiàng)目目錄(為保證確認(rèn),可以將路徑同時添加到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應(yīng)用下web.xml文件添加相應(yīng)的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_Cafeconf</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_Cafeconf</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>

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd