第一個 Cactus + Jetty 測試[轉]

          轉自:http://www.cntesting.com/pic/study/JUnitGossip/FirstCactusWithJetty.htm
          在Tomcat或您的目標Container上直接執行測試的好處是,您即完成了單元測試,也測試了您的單元與 Container的交互,然而比較麻煩的是,您必須在每一次的修改之後,重新布署相關的資源、啟動Container、運行測試等等,測試時較為耗時耗力。

          對於簡單的In-Container單元測試,您可以使用stub的方式,stub即將真實系統的一部份引入您的程式之中,讓您的程式可以與這一個部份進行交互,而不一定要將整個程式置於系統之中。

          stub的好處是,有時您并不是要測試程式與整個系統的行為,并且您也不是每次都可以將程式丟到系統之上運行,試想,您不能為了測試您的單元,而要求真正在服務客戶的系統不斷的重啟。

          對於In-Container測試采取stub的方式,自然就是實現Container的部份功能,并將測試置於其中,在這邊您可以使用 Jetty [http://jetty.mortbay.org/jetty/index.html],它是個Java撰寫的HTTP伺服器,本身也是個 Container,Cactus集成了Jetty,并提供與測試相關的簡便類別。

          使用Cactus+Jetty執行測試,在更大的程度上隱藏了測試運行過程的細節,您不必關心Redirector Proxy,更不一定要關心TestCase在客戶端與伺服端的行為,運行起來就如同在運作一個JUnit測試。

          使用Cactus+Jetty進行測試時,Jetty會在測試開始前完成啟動,接著進行相關測試,然後Jetty會自動關閉,這很方便,另一方面,啟動 Jetty會快的多了。

          要使用Cactus+Jetty,請將Cactus下載後的lib目錄中的commons-logging-xxx.jar、 aspectjrt-xxx.jar、cactus-xxx.jar、commons-httpclient-xxx.jar、junit- xxx.jar以及org.mortbay.jetty-xxx.jar設定至CLASSPATH。

          接著撰寫測試案例:


          • LoginServletTest.java
          package onlyfun.caterpillar.test;

          import junit.framework.Test;
          import junit.framework.TestSuite;

          import org.apache.cactus.ServletTestCase;
          import org.apache.cactus.WebRequest;
          import org.apache.cactus.extension.jetty.JettyTestSetup;

          import onlyfun.caterpillar.LoginServlet;

          public class LoginServletTest extends ServletTestCase {
          public static Test suite() {
          System.setProperty("cactus.contextURL",
          "http://localhost:8080/cactusDemo");
          TestSuite suite = new TestSuite();
          suite.addTestSuite(LoginServletTest.class);
          return new JettyTestSetup(suite);
          }

          public void beginValidUser(WebRequest webRequest) {
          webRequest.addParameter("username", "justin");
          webRequest.addParameter("password", "123456");
          }

          public void testValidUser() {
          LoginServlet loginServlet = new LoginServlet();
          assertTrue(loginServlet.isValidUser(request));
          }

          public void beginInValidUser(WebRequest webRequest) {
          webRequest.addParameter("username", "guest");
          webRequest.addParameter("password", "123456");
          }

          public void testInValidUser() {
          LoginServlet loginServlet = new LoginServlet();
          assertFalse(loginServlet.isValidUser(request));
          }

          public static void main(String[] args) {
          junit.textui.TestRunner.run(
          LoginServletTest.suite());
          }
          }

          在這邊要特別注意的是suite()方法,傳回了一個JettyTestSetup實例,如您所想的,這個實例除了運行TestSuite之外,它還會啟動Jetty。接下來依測試案例來完成程式:
          • LoginServlet.java
          package onlyfun.caterpillar;

          import javax.servlet.http.*;

          public class LoginServlet extends HttpServlet {
          public boolean isValidUser(HttpServletRequest request) {
          String username = request.getParameter("username");
          String password = request.getParameter("password");

          if(username == null ||
          password == null ||
          !username.equals("justin") ||
          !password.equals("123456")) {
          return false;
          }
          else {
          return true;
          }
          }
          }

          然後就可以運行測試了,以下是測試的結果:
          09:26:10.625 EVENT? Starting Jetty/4.2.17
          09:26:10.843 EVENT? Started ServletHttpContext[/cactusDemo]
          09:26:39.203 EVENT? Started SocketListener on 0.0.0.0:8080
          09:26:39.203 EVENT? Started org.mortbay.jetty.Server@758fc9
          ..09:26:40.296 EVENT? Stopping Acceptor?
          ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8080]
          09:26:40.296 EVENT? Stopped SocketListener on 0.0.0.0:8080
          09:26:40.296 EVENT? Stopped ServletHttpContext[/cactusDemo]
          09:26:40.296 EVENT? Stopped org.mortbay.jetty.Server@758fc9

          Time: 31.453

          OK (2 tests)



          還可以參考http://kb.csdn.net/java/Articles/200312/2a86aa04-cbf1-4eda-98a4-902b1c974cc7.html
          http://www.51testing.com/html/6/23.html

          posted on 2006-03-02 09:09 扭轉乾坤 閱讀(385) 評論(0)  編輯  收藏 所屬分類: JAVA使用技巧

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類(31)

          隨筆檔案(30)

          文章分類(32)

          文章檔案(33)

          相冊

          PHP小站-首頁

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 太谷县| 济阳县| 瑞昌市| 衢州市| 越西县| 平乡县| 从江县| 三原县| 柳州市| 玉田县| 平原县| 平山县| 南丹县| 哈尔滨市| 定州市| 上杭县| 井冈山市| 尼木县| 施秉县| 武威市| 绥棱县| 宝清县| 彩票| 东乡| 木里| 科技| 绿春县| 黄石市| 井冈山市| 桐城市| 高雄县| 隆子县| 绿春县| 泰宁县| 西宁市| 新蔡县| 阿拉尔市| 郓城县| 收藏| 义马市| 台东市|