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

. 簡(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)單的,你寫(xiě)的測(cè)試類(lèi)只需繼承ServletTestCase或者JspTestCase、FilterTestCase(它們都繼承了JUnit的TestCase)。寫(xiě)好測(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下,我們寫(xiě)的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è)試類(lèi)的完整代碼框架,其中的extends部分需要按你所測(cè)試的不同目標(biāo)來(lái)繼承不同的類(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是客戶(hù)端代碼,所以在這兩個(gè)方法里是無(wú)法使用request這樣的服務(wù)端對(duì)象的。

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

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

可以看到區(qū)別在于引用的包不同,為什么會(huì)這樣的呢?因?yàn)樵趘1.2開(kāi)始Cactus集成了HttpUnit這個(gè)組件。如果你熟悉HttpUnit這個(gè)組件,我想應(yīng)該明白為什么要集成HttpUnit。下面我們來(lái)看一段代碼開(kā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的差別可以參見(jiàn)兩者各自的API Doc,這里不再多說(shuō)了。

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

. 寫(xiě)測(cè)試代碼

首先,我們給出被測(cè)類(lèi)的代碼,是一個(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è)試類(lèi)框架已經(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"));

}

}

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