<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Cactus實(shí)例講解(轉(zhuǎn)載)

    Cactus簡(jiǎn)介

    . 簡(jiǎn)介

    Cactus實(shí)現(xiàn)了對(duì)JUnit測(cè)試框架的無(wú)縫擴(kuò)展,可以方便地測(cè)試服務(wù)端應(yīng)用程序。Cactus可以在下面幾種情況下使用:

    • 測(cè)試Servlet以及任何使用了像HttpServletRequest,HttpServletResponse,……這樣的對(duì)象的代碼。使用ServletTestCase。
    • 測(cè)試Filter以及任何使用了像FilterConfig,……這樣的對(duì)象的代碼。使用FilterTestCase。
    • 測(cè)試JSP 。使用ServletTestCase或JspTestCase。
    • 測(cè)試Taglibs以及任何使用了像PageContext,……這樣的對(duì)象的代碼。使用JspTestCase。
    • 測(cè)試EJB。ServletTestCase或JspTestCase或FilterTestCase。

    Cactus的使用也是非常簡(jiǎn)單的,你寫的測(cè)試類只需繼承ServletTestCase或者JspTestCase、FilterTestCase(它們都繼承了JUnit的TestCase)。寫好測(cè)試代碼后需要啟動(dòng)web容器,然后執(zhí)行測(cè)試代碼。在下面的章節(jié)中我們將通過(guò)例子向你詳細(xì)講解。

    Cactus項(xiàng)目Apache Jakarta Commons的一個(gè)子項(xiàng)目,網(wǎng)址是:http://jakarta.apache.org/commons/cactus/

    . TestCase框架

    在Cactus下,我們寫的TestCase與JUnit有所不同,先看一段代碼,如下:

           public class TestSample extendsServletTestCase/JspTestCase/FilterTestCase {
           public TestSample (String testName) {
           super(testName);
           }
           public void setUp() {
           }
           public void tearDown() {
           }
           public void beginXXX(WebRequest theRequest) {
           }
           public void testXXX() {
           }
           public void endXXX(WebResponse theResponse) {
           }

    上面是一個(gè)Cactus測(cè)試類的完整代碼框架,其中的extends部分需要按你所測(cè)試的不同目標(biāo)來(lái)繼承不同的類(簡(jiǎn)介中有所描述)。

    另外我們注意到兩個(gè)新的方法beginXXX和endXXX的,這兩個(gè)方法分別會(huì)在testXXX執(zhí)行前和執(zhí)行后執(zhí)行,它們和setUp、tearDown不同的是beginXXX和endXXX會(huì)在相應(yīng)的testXXX前執(zhí)行,而setUp和tearDown則在每個(gè)testXXX方法前都會(huì)執(zhí)行。另外beginXXX和endXXX是客戶端代碼,所以在這兩個(gè)方法里是無(wú)法使用request這樣的服務(wù)端對(duì)象的。

    對(duì)于endXXX方法需要另加說(shuō)明的是,在Cactus v1.1前(包括v1.1),它的形式是這樣的public void endXXX(HttpURLConnection theConnection),而在Cactus v1.2開始它的形式有兩種可能:

    • public void endXXX(org.apache.cactus.WebResponse theResponse);
    • public void endXXX(com.meterware.httpunit.WebResponse theResponse);

    可以看到區(qū)別在于引用的包不同,為什么會(huì)這樣的呢?因?yàn)樵趘1.2開始Cactus集成了HttpUnit這個(gè)組件。如果你熟悉HttpUnit這個(gè)組件,我想應(yīng)該明白為什么要集成HttpUnit。下面我們來(lái)看一段代碼開比較一下兩者的區(qū)別:

    public void endXXX(org.apache.cactus.WebResponse theResponse) {

    String content = theResponse.getText();

    assertEquals(content, "<html><body><h1>Hello world!</h1></body></html>");

    }

    public void endXXX(com.meterware.httpunit.WebResponse theResponse) {

    WebTable table = theResponse.getTables()[0];

    assertEquals("rows", 4, table.getRowCount());

    assertEquals("columns", 3, table.getColumnCount());

    assertEquals("links", 1, table.getTableCell(0, 2).getLinks().length);

    }

    當(dāng)然,在實(shí)際應(yīng)用中你需要根據(jù)不同的需要來(lái)選擇不同的endXXX。兩個(gè)WebResponse的差別可以參見兩者各自的API Doc,這里就不再多說(shuō)了。

    如何在Cactus里寫測(cè)試

    . 寫測(cè)試代碼

    首先,我們給出被測(cè)類的代碼,是一個(gè)Servlet:

    public class SampleServlet extends HttpServlet {

    public void doGet(HttpServletRequest theRequest,

    HttpServletResponse theResponse) throws IOException {

    PrintWriter pw = theResponse.getWriter();

    theResponse.setContentType("text/html");

    pw.print("<html><head/><body>");

    pw.print("A GET request");

    pw.print("</body></html>");

    }

    public String checkMethod(HttpServletRequest theRequest) {

    return theRequest.getMethod();

    }

    }

    Cactus中的測(cè)試類框架已經(jīng)在上面給出。下面來(lái)看一下例子,例子是從中Cactus自帶的實(shí)例中抽取的一部分,如下:

    public class TestSampleServlet extends ServletTestCase {

    public void testReadServletOutputStream() throws IOException {

    SampleServlet servlet = new SampleServlet();

    servlet.doGet(request, response);

    }

    public void endReadServletOutputStream(WebResponse theResponse)

    throws IOException {

    String expected = "<html><head/><body>A GET request</body></html>";

    String result = theResponse.getText();

    assertEquals(expected, result);

    }

    public void beginPostMethod(WebRequest theRequest) {

    theRequest.addParameter("param", "value", WebRequest.POST_METHOD);

    }

    public void testPostMethod() {

    SampleServlet servlet = new SampleServlet();

    assertEquals("POST", servlet.checkMethod(request));

    assertEquals("value", request.getParameter("param"));

    }

    }

    第一個(gè)方法testReadServletOutputStream,調(diào)用doGet,相當(dāng)于在客戶端提交請(qǐng)求,然后在Servlet處理后會(huì)產(chǎn)生一個(gè)回饋,所以,在endReadServletOutputStream方法里,我們通過(guò)調(diào)用response的相應(yīng)方法判斷回饋是否符合預(yù)期結(jié)果。

    第二個(gè)方法testPostMethod,在這之前有一個(gè)beginPostMethod,在這個(gè)方法里我們以POST方式往request里增加一個(gè)表單數(shù)據(jù)param,值為”value”。下面在testPostMethod我們就要驗(yàn)證表單數(shù)據(jù)是否以POST方式提交到了服務(wù)端的Servlet里,所以,我們看到了兩個(gè)assertEquals,分別進(jìn)行了判斷。在這里我們要注意到beginPostMethod方法中的theRequest和testPostMethod中的request的區(qū)別,在前面我們已經(jīng)提到過(guò),beginPostMethod是在客戶端執(zhí)行的,所以它方法內(nèi)的所有操作事實(shí)上是模擬頁(yè)面操作的,比如上面的設(shè)置表單數(shù)據(jù),而testPostMethod是服務(wù)端執(zhí)行的,其中的request也是服務(wù)端的。

    配置cactus.properties和web.xml

    cactus.properties

    • cactus.contextURL

    這個(gè)屬性是必須的,它指定了web應(yīng)用的訪問(wèn)地址

    例:cactus.contextURL = http://localhost:8080/test

    • cactus.servletRedirectorName

    可選,當(dāng)測(cè)試類繼承ServletTestCase時(shí)用于指定Cactus Servlet Redirector的映射名稱。默認(rèn):ServletRedirector

    例:cactus.servletRedirectorName = ServletRedirector

    • cactus.jspRedirectorName

    可選,當(dāng)測(cè)試類繼承ServletTestCase時(shí)用于指定Cactus Jsp Redirector的映射名稱。默認(rèn):ServletRedirector

    例:cactus.jspRedirectorName = JspRedirector

    可選,當(dāng)測(cè)試類繼承ServletTestCase時(shí)用于指定Cactus Filter Redirector的映射名稱。默認(rèn):ServletRedirector

    例:cactus.filterRedirectorName = FilterRedirector

    Cactus.properties你可以放置在WEB-INF/classes/下。

    web.xml

    在web.xml里要為相應(yīng)的測(cè)試類指定相應(yīng)的Cactus Redirector。

    ServletTestCase對(duì)應(yīng)org.apache.cactus.server.ServletTestRedirector

    JspTestCase對(duì)應(yīng)/jspRedirector.jsp

    FilterTestCase對(duì)應(yīng)org.apache.cactus.server.FilterTestRedirector

    <web-app>

    <filter>

    <filter-name>FilterRedirector</filter-name>

    <filter-class>org.apache.cactus.server.FilterTestRedirector</filter-class>

    </filter>

    <filter-mapping>

    <filter-name>FilterRedirector</filter-name>

    <url-pattern>/FilterRedirector</url-pattern>

    </filter-mapping>

    <servlet>

    <servlet-name>ServletRedirector</servlet-name>

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

    </servlet>

    <servlet>

    <servlet-name>JspRedirector</servlet-name>

    <jsp-file>/jspRedirector.jsp</jsp-file>

    </servlet>

    <servlet-mapping>

    <servlet-name>ServletRedirector</servlet-name>

    <url-pattern>/ServletRedirector</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

    <servlet-name>JspRedirector</servlet-name>

    <url-pattern>/JspRedirector</url-pattern>

    </servlet-mapping>

    </web-app>

    如果你的測(cè)試類繼承了JspTestCase則需要將jspRedirector.jsp文件放置到你在web.xml中指定的路徑里。

    安裝說(shuō)明

    • 在使用Cactus時(shí),strutstest.jar還需要有下列包的支持。包可放置在WEB-INF/lib下

    如下:

    junit.jar

    servlet.jar

    cactus.jar

    httpclient.jar

    commons-logging.jar

    httpunit.jar,Tidy.jar,xerces.jar(可選,如果你集成了httpunit的話就需要,也就是在endXXX中使用了httpunit)

      • Server端(也就是web容器)需要如下包

    cactus.jar

    junit.jar

    aspectjrt.jar

    commons-logging.jar

    • 寫好測(cè)試代碼后將class放置在WEB-INF/classes下
    • 被測(cè)代碼也放置在WEB-INF/classes下
    • 寫好cactus.properties和web.xml兩個(gè)配置文件
    • 啟動(dòng)web容器
    • 運(yùn)行測(cè)試代碼


    posted on 2006-06-12 09:34 nbt 閱讀(269) 評(píng)論(0)  編輯  收藏 所屬分類: Java2EE

    <2006年6月>
    28293031123
    45678910
    11121314151617
    18192021222324
    2526272829301
    2345678

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊(cè)

    收藏夾

    Java技術(shù)網(wǎng)站

    友情鏈接

    國(guó)內(nèi)一些開源網(wǎng)站

    最新隨筆

    搜索

    積分與排名

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲无码精品浪潮| 亚洲国产成人久久综合| 免费一级特黄特色大片在线| 久久国产免费观看精品3| 一个人看的在线免费视频| 亚洲成av人无码亚洲成av人| 亚洲国产日韩在线成人蜜芽| 国产亚洲精品无码成人| 亚洲AV日韩精品一区二区三区| 成人黄动漫画免费网站视频| 最近中文字幕大全中文字幕免费| 青青操免费在线视频| 亚州**色毛片免费观看| 国产综合成人亚洲区| 亚洲日韩一区二区三区| 亚洲a∨无码男人的天堂| 亚洲成aⅴ人片在线影院八| 亚洲伊人tv综合网色| 久久亚洲AV午夜福利精品一区 | 国产V亚洲V天堂无码| 中文字幕亚洲激情| 亚洲乱码中文字幕手机在线| 四虎影视精品永久免费| 国产性生交xxxxx免费| 国产区卡一卡二卡三乱码免费| 成人性生免费视频| 成年女人色毛片免费看| 无码国产精品久久一区免费 | 亚洲人成在久久综合网站| 中文字幕亚洲精品| 亚洲精品国产手机| 78成人精品电影在线播放日韩精品电影一区亚洲 | 4虎永免费最新永久免费地址| 久热中文字幕在线精品免费| 老司机在线免费视频| 成年在线网站免费观看无广告| 91在线视频免费91| 拔擦拔擦8x华人免费久久 | 五月婷婷免费视频| 中文字幕永久免费视频| 性xxxxx大片免费视频|