• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    Java單體測試工具cactus使用指南

    發布: 2007-7-01 18:47 | 作者: admin | 來源: | 查看: 42次 | 進入軟件測試論壇討論

    領測軟件測試網 1 cactus測試的原理
    看下面這張圖,這是一個測試工具Cactus測試Servlet的過程,而在過去測試Servlet是很麻煩的。
    1.首先,JUnit測試器執行YYYTestCase.runTest(),這個函數找到beginXXX(WebRequest)執行它,這個過程發生在客戶端。WebRequest作為參數,它包含了請求的HTTP頭、HTTP參數,它將被傳送到第2步的重定向代理器中。
    2.YYYTestCase.runTest()建立一個到重定向代理器的HTTP鏈接. 上一步的WebRequest傳了過去。
    3.從客戶端來看,重定向代理器好像是在服務器運行一樣。這意味著你的測試用例類將被實例兩次:一次是在客戶端(被JUnit測試器實例),另一次是在服務器端(被重定向代理器實例)?蛻舳说膶嵗脕韴绦衎eginXXX()和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請參照相關的文檔,在文件目錄下均存在

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>