posts - 14,  comments - 37,  trackbacks - 0
          在Tomcat或您的目標(biāo)Container上直接執(zhí)行測試的好處是,您即完成了單元測試,也測試了您的單元與 Container的交互,然而比較麻煩的是,您必須在每一次的修改之後,重新布署相關(guān)的資源、啟動Container、運(yùn)行測試等等,測試時較為耗時耗力。

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

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

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

          使用Cactus+Jetty執(zhí)行測試,在更大的程度上隱藏了測試運(yùn)行過程的細(xì)節(jié),您不必關(guān)心Redirector Proxy,更不一定要關(guān)心TestCase在客戶端與伺服端的行為,運(yùn)行起來就如同在運(yùn)作一個JUnit測試。

          使用Cactus+Jetty進(jìn)行測試時,Jetty會在測試開始前完成啟動,接著進(jìn)行相關(guān)測試,然後Jetty會自動關(guān)閉,這很方便,另一方面,啟動 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設(shè)定至CLASSPATH。

          接著撰寫測試案例:
          • LoginServletTest.java

           

           1package onlyfun.caterpillar.test;
           2
           3import junit.framework.Test;
           4import junit.framework.TestSuite;
           5import org.apache.cactus.ServletTestCase;
           6import org.apache.cactus.WebRequest;
           7import org.apache.cactus.extension.jetty.JettyTestSetup;
           8import onlyfun.caterpillar.LoginServlet;
           9
          10public class LoginServletTest extends ServletTestCase
          11{
          12    public static Test suite()
          13    {
          14        System.setProperty("cactus.contextURL""http://localhost:8080/cactusDemo");
          15        TestSuite suite = new TestSuite();
          16        suite.addTestSuite(LoginServletTest.class);
          17        return new JettyTestSetup(suite);
          18    }

          19
          20    public void beginValidUser(WebRequest webRequest)
          21    {
          22        webRequest.addParameter("username""justin");
          23        webRequest.addParameter("password""123456");
          24    }

          25
          26    public void testValidUser()
          27    {
          28        LoginServlet loginServlet = new LoginServlet();
          29        assertTrue(loginServlet.isValidUser(request));
          30    }

          31
          32    public void beginInValidUser(WebRequest webRequest)
          33    {
          34        webRequest.addParameter("username""guest");
          35        webRequest.addParameter("password""123456");
          36    }

          37
          38    public void testInValidUser()
          39    {
          40        LoginServlet loginServlet = new LoginServlet();
          41        assertFalse(loginServlet.isValidUser(request));
          42    }

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

          48}

          49


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

           

           1package onlyfun.caterpillar;
           2
           3import javax.servlet.http.*;
           4
           5public class LoginServlet extends HttpServlet
           6{
           7    public boolean isValidUser(HttpServletRequest request)
           8    {
           9        String username = request.getParameter("username");
          10        String password = request.getParameter("password");
          11        if (username == null || password == null || !username.equals("justin")
          12                || !password.equals("123456"))
          13        {
          14            return false;
          15        }

          16        else
          17        {
          18            return true;
          19        }

          20    }

          21}

          22
          然後就可以運(yùn)行測試了,以下是測試的結(jié)果:
           109:26:10.625 EVENT  Starting Jetty/4.2.17
           209:26:10.843 EVENT  Started ServletHttpContext[/cactusDemo]
           309:26:39.203 EVENT  Started SocketListener on 0.0.0.0:8080
           409:26:39.203 EVENT  Started org.mortbay.jetty.Server@758fc9
           5..09:26:40.296 EVENT  Stopping Acceptor  
           6ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8080]
           709:26:40.296 EVENT  Stopped SocketListener on 0.0.0.0:8080
           809:26:40.296 EVENT  Stopped ServletHttpContext[/cactusDemo]
           909:26:40.296 EVENT  Stopped org.mortbay.jetty.Server@758fc9
          10
          11Time: 31.453
          12
          13OK (2 tests)

          如您所看到的,整個測試過程相當(dāng)?shù)暮喴祝珻actus+Jetty隱藏了更多的細(xì)節(jié),您測試的行為幾乎與只使用JUnit時是一致的。
          posted on 2007-07-10 10:19 冰封的愛 閱讀(303) 評論(0)  編輯  收藏 所屬分類: J2EE
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 永济市| 德安县| 鄢陵县| 蛟河市| 鄂州市| 黔西县| 称多县| 建湖县| 桂阳县| 齐齐哈尔市| 深泽县| 新密市| 阿城市| 祁阳县| 青神县| 华安县| 通化市| 舟曲县| 山东省| 清水河县| 安溪县| 西盟| 芦山县| 汝阳县| 积石山| 南丰县| 阿勒泰市| 沅江市| 山东| 县级市| 乡宁县| 洛阳市| 揭阳市| 观塘区| 新民市| 铅山县| 济南市| 巩义市| 亳州市| 漳平市| 正镶白旗|