Cactus實例講解(轉載)

          Cactus簡介

          . 簡介

          Cactus實現(xiàn)了對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容器,然后執(zhí)行測試代碼。在下面的章節(jié)中我們將通過例子向你詳細講解。

          Cactus項目Apache Jakarta Commons的一個子項目,網(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部分需要按你所測試的不同目標來繼承不同的類(簡介中有所描述)。

          另外我們注意到兩個新的方法beginXXX和endXXX的,這兩個方法分別會在testXXX執(zhí)行前和執(zhí)行后執(zhí)行,它們和setUp、tearDown不同的是beginXXX和endXXX會在相應的testXXX前執(zhí)行,而setUp和tearDown則在每個testXXX方法前都會執(zhí)行。另外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);

          可以看到區(qū)別在于引用的包不同,為什么會這樣的呢?因為在v1.2開始Cactus集成了HttpUnit這個組件。如果你熟悉HttpUnit這個組件,我想應該明白為什么要集成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);

          }

          當然,在實際應用中你需要根據(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自帶的實例中抽取的一部分,如下:

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

          }

          }

          第一個方法testReadServletOutputStream,調用doGet,相當于在客戶端提交請求,然后在Servlet處理后會產(chǎn)生一個回饋,所以,在endReadServletOutputStream方法里,我們通過調用response的相應方法判斷回饋是否符合預期結果。

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

          配置cactus.properties和web.xml

          cactus.properties

          • cactus.contextURL

          這個屬性是必須的,它指定了web應用的訪問地址

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

          • cactus.servletRedirectorName

          可選,當測試類繼承ServletTestCase時用于指定Cactus Servlet Redirector的映射名稱。默認:ServletRedirector

          例:cactus.servletRedirectorName = ServletRedirector

          • cactus.jspRedirectorName

          可選,當測試類繼承ServletTestCase時用于指定Cactus Jsp Redirector的映射名稱。默認:ServletRedirector

          例:cactus.jspRedirectorName = JspRedirector

          可選,當測試類繼承ServletTestCase時用于指定Cactus Filter Redirector的映射名稱。默認:ServletRedirector

          例:cactus.filterRedirectorName = FilterRedirector

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

          web.xml

          在web.xml里要為相應的測試類指定相應的Cactus Redirector。

          ServletTestCase對應org.apache.cactus.server.ServletTestRedirector

          JspTestCase對應/jspRedirector.jsp

          FilterTestCase對應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>

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

          安裝說明

          • 在使用Cactus時,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

          • 寫好測試代碼后將class放置在WEB-INF/classes下
          • 被測代碼也放置在WEB-INF/classes下
          • 寫好cactus.properties和web.xml兩個配置文件
          • 啟動web容器
          • 運行測試代碼


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

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

          導航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java技術網(wǎng)站

          友情鏈接

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

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 观塘区| 临城县| 南召县| 贡嘎县| 随州市| 康乐县| 深泽县| 女性| 琼海市| 五指山市| 陆川县| 安图县| 土默特左旗| 平昌县| 彭阳县| 古田县| 威宁| 宜阳县| 五莲县| 江源县| 老河口市| 泗洪县| 普定县| 吉木乃县| 安阳县| 惠来县| 卓资县| 苍山县| 荆州市| 志丹县| 荃湾区| 德惠市| 山阴县| 朝阳县| 五原县| 木里| 罗江县| 内乡县| 邵武市| 博爱县| 陵川县|