亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > Cactus
Cactus實(shí)例講解
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/3/5 13:14:37 ] 推薦標(biāo)簽:

. 簡介

Cactus實(shí)現(xiàn)了對JUnit測試框架的無縫擴(kuò)展,可以方便地測試服務(wù)端應(yīng)用程序。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容器,然后執(zhí)行測試代碼。在下面的章節(jié)中我們將通過例子向你詳細(xì)講解。

Cactus項(xiàng)目Apache Jakarta Commons的一個子項(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) {
       }

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

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

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

可以看到區(qū)別在于引用的包不同,為什么會這樣的呢?因?yàn)樵趘1.2開始Cactus集成了HttpUnit這個組件。如果你熟悉HttpUnit這個組件,我想應(yīng)該明白為什么要集成HttpUnit。下面我們來看一段代碼開比較一下兩者的區(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ù)不同的需要來選擇不同的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中的測試類框架已經(jīng)在上面給出。下面來看一下例子,例子是從中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"));

}

}

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 日韩专区亚洲国产精品 | 在线成人欧美 | 国产精品www | 深夜福利网 | 日本一区二区三区免费高清在线 | 亚洲第一免费视频 | 青草草| 日本大黄网站 | 很黄很色的男女无遮挡动态图 | 日本免费高清一区 | 一级毛片短视频 | 深夜在线看片 | 五月天狠狠干 | 黑人极品videos精品欧美裸 | 在线免费观看色片 | 天堂网在线免费 | 91av国产在线 | 国产高清a毛片在线看 | 日本成人一级片 | 亚洲 欧美 激情 另类 自拍 | 成年人免费观看的视频 | 久99久精品视频免费观看v | 欧美综合国产精品日韩一 | 欧美成人精品手机在线观看 | 亚洲青草 | 嗯男人边吃奶边做边爱视频 | 欧美日韩成人在线视频 | 精品国产成人在线 | 黄色片视频在线免费观看 | 国产一及毛片 | 成人涩涩视频 | 国产亚洲精品hd网站 | 中文字幕成人免费高清在线视频 | 亚洲国产福利精品一区二区 | 免费不卡中文字幕在线 | 久久黄色一级片 | 开心久久激情 | 午夜精品久视频在线观看 | 91看片淫黄大片在看 | 亚洲综合天堂网 | 中文字幕在线视频播放 |