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

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

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

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

    軟件測試之Cactus實例講解

    發布: 2009-3-11 10:31 | 作者: 不詳 | 來源: 測試時代采編 | 查看: 252次 | 進入軟件測試論壇討論

    領測軟件測試網

    Cactus簡介

    . 簡介

    Cactus實現了對JUnit測試框架的無縫擴展,可以方便地測試服務端應用程序。Cactus可以在下面幾種情況下使用:

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

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

    Cactus項目Apache Jakarta Commons的一個子項目,網址是: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) {
           }

    上面是一個Cactus測試類的完整代碼框架,其中的extends部分需要按你所測試的不同目標來繼承不同的類(簡介中有所描述)。

    另外我們注意到兩個新的方法beginXXX和endXXX的,這兩個方法分別會在testXXX執行前和執行后執行,它們和setUp、tearDown不同的是beginXXX和endXXX會在相應的testXXX前執行,而setUp和tearDown則在每個testXXX方法前都會執行。另外beginXXX和endXXX是客戶端代碼,所以在這兩個方法里是無法使用request這樣的服務端對象的。

    對于endXXX方法需要另加說明的是,在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);

    可以看到區別在于引用的包不同,為什么會這樣的呢?因為在v1.2開始Cactus集成了HttpUnit這個組件。如果你熟悉HttpUnit這個組件,我想應該明白為什么要集成HttpUnit。下面我們來看一段代碼開比較一下兩者的區別:

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

    }

    當然,在實際應用中你需要根據不同的需要來選擇不同的endXXX。兩個WebResponse的差別可以參見兩者各自的API Doc,這里就不再多說了。

    如何在Cactus里寫測試

    . 寫測試代碼

    首先,我們給出被測類的代碼,是一個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中的測試類框架已經在上面給出。下面來看一下例子,例子是從中Cactus自帶的實例中抽取的一部分,如下:

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

    }

    延伸閱讀

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

    TAG: Cactus 講解 軟件測試 實例

    21/212>

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