qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          使用Cactus+Jetty進行servelt的單元測試

          今天在做《Junit In Action》關(guān)于cactus和jetty結(jié)合進行集成測試的例子,看看源代碼很簡單,但總是運行不起來,一波三折了好幾個小時才搞定。我用的cactus是1.8.1,閑言少敘,上源代碼,就2個類:

            待測試的servlet:

          package junitbook.container;

          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpSession;

          public class SampleServlet extends HttpServlet
          {
              public boolean isAuthenticated(HttpServletRequest request)
              {
                  HttpSession session = request.getSession(false);

                  if (session == null)
                  {
                      return false;
                  }
                 
                  String authenticationAttribute =
                      (String) session.getAttribute("authenticated");
                     
                  return Boolean.valueOf(
                      authenticationAttribute).booleanValue();
              }
          }




            測試類:

          package junitbook.container;

          import org.apache.cactus.ServletTestCase;
          import org.apache.cactus.WebRequest;

          public class TestSampleServletIntegration extends ServletTestCase
          {
              private SampleServlet servlet;   

              public static Test suite()
              {
                  System.setProperty("cactus.contextURL", "http://localhost:8080/test");// 這步很重要,一定要有

                  TestSuite suite = new TestSuite("All tests with Jetty");
                  suite.addTestSuite(TestSampleServletIntegration.class);
                  return new Jetty5XTestSetup(suite);
              }

              protected void setUp()
              {
                  servlet = new SampleServlet();
              }
              
              public void testIsAuthenticatedAuthenticated()
              {
                  session.setAttribute("authenticated", "true");
                  
                  assertTrue(servlet.isAuthenticated(request));
              }

              public void testIsAuthenticatedNotAuthenticated()
              {
                  assertFalse(servlet.isAuthenticated(request));
              }

              public void beginIsAuthenticatedNoSession(WebRequest request)
              {
                  request.setAutomaticSession(false);
              }
              
              public void testIsAuthenticatedNoSession()
              {
                  assertFalse(servlet.isAuthenticated(request));
              }
          }

            有幾點要說明:

            1.測試類要繼承ServletTestCase,內(nèi)置了session,request,response,可直接用

            2.jetty不要用單獨下載的版本,每個cactus發(fā)行版都內(nèi)置了一個jetty,在lib目錄下可以找到,顯然是改寫過的,如果你用單獨下載的,一定會拋出java.lang.NoSuchMethodException: org.mortbay.jetty.nio.SelectChannelConnector.setPort(java.lang.String),因為在單獨的jetty發(fā)行版中,setPort用的參數(shù)是int,所以會拋異常,不知cactus為什么要這樣搞。

            3.還要依賴commons codec這個jar

            4.如果jetty是5.x,用Jetty5XTestSetup,如果是6.x,用Jetty6XTestSetup,不過當(dāng)前這個cactus用的還是jetty5.1.9(org.mortbay.jetty-5.1.9.jar),所以就用Jetty5XTestSetup。

            就這些,jetty啟動很快,過段時間還要研究一下怎么來測試struts2。

          posted on 2013-09-22 11:09 順其自然EVO 閱讀(252) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 于田县| 衡东县| 金寨县| 田阳县| 阳谷县| 日照市| 贵德县| 连南| 宁波市| 武穴市| 吉木萨尔县| 岳普湖县| 冷水江市| 象州县| 兴山县| 湖州市| 资溪县| 奈曼旗| 湟源县| 安陆市| 衡水市| 色达县| 遂溪县| 临汾市| 綦江县| 太仓市| 河西区| 岳池县| 桦甸市| 井研县| 东辽县| 重庆市| 兴业县| 康保县| 铜陵市| 杭锦旗| 河津市| 乌拉特后旗| 五指山市| 惠东县| 南岸区|