posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          測試 WEB

          Posted on 2007-11-02 12:44 G_G 閱讀(1016) 評論(0)  編輯  收藏 所屬分類: JUnit

          學習測試留個印


          測試 Servlet 使用:

          package ?jetty.test.supper;

          import ?java.io.File;

          import ?javax.servlet.http.HttpServletRequest;

          import ?jetty.LoginServlet;

          import ?com.meterware.httpunit.GetMethodWebRequest;
          import ?com.meterware.httpunit.WebConversation;
          import ?com.meterware.httpunit.WebRequest;
          import ?com.meterware.httpunit.WebResponse;
          import ?com.meterware.servletunit.InvocationContext;
          import ?com.meterware.servletunit.ServletRunner;
          import ?com.meterware.servletunit.ServletUnitClient;

          import ?junit.framework.TestCase;

          public ? class ?HttpUnitTest? extends ?TestCase?{
          ????
          public ? void ?ctestUrl() throws ?Exception{
          ????????System.out.println(
          " 直接獲取網頁內容: " );
          ????????WebConversation?wc?
          = ? new ?WebConversation();
          ????????WebResponse?wr?
          = ?wc.getResponse(? " http://www.google.com " ?);
          ????????System.out.println(?wr.getText()?);
          ????}
          ????
          ????
          public ? void ?testRun1() throws ?Exception{
          ????????System.out.println(
          " 無參數測試: " );
          ????????
          // 建立服務器
          ????????ServletRunner?sr? = ? new ?ServletRunner( new ?File( " WebRoot\\WEB-INF\\test.xml " ));
          ????????
          // 模擬客戶端
          ????????ServletUnitClient?sc? = ?sr.newClient();
          ????????
          // URL?定位
          ????????WebRequest?request??? = ? new ?GetMethodWebRequest(? " http://localhost:7000/cactusDemo " ?);
          ????????WebResponse?response?
          = ?sc.getResponse(?request?);
          ????????
          // 運行
          ????????System.out.println(response.getText() + " \n " );
          ????}
          ????
          ????
          public ? void ?testRun2() throws ?Exception{
          ????????System.out.println(
          " Session?和?request?測試: " );
          ????????ServletRunner?sr?
          = ? new ?ServletRunner( new ?File( " WebRoot\\WEB-INF\\test.xml " ));
          ????????ServletUnitClient?sc?
          = ?sr.newClient();
          ????????WebRequest?request???
          = ? new ?GetMethodWebRequest(? " http://localhost/cactusDemo " ?);
          ????????
          // Parameter?加參?use?
          ????????request.setParameter( " use " , " web " );
          ????????
          ????????
          // 取出?Servlet?:?LoginServlet?方法準備執行
          ????????InvocationContext?ic? = ?sc.newInvocation(?request?);
          ????????LoginServlet?se?
          = ?(LoginServlet)ic.getServlet();
          ????????HttpServletRequest?re?
          = ?ic.getRequest()?;

          ????????
          // request?session??加參?use?
          ????????re.setAttribute( " use " , " re " );
          ????????re.getSession(
          true ).setAttribute( " use " , " re " );

          ????????se.doGet(re,ic.getResponse());
          // 關鍵:運行測試方法
          ????????
          ????????
          // 獲取?out
          ????????WebResponse?response? = ?ic.getServletResponse();
          ????????System.out.println(response.getText());
          ????}
          }

          test.xml

          <? xml?version="1.0"?encoding="UTF-8" ?>
          < web-app? xmlns ="http://java.sun.com/xml/ns/j2ee" ?xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" ?version ="2.4" ?xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee???http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
          ??
          < servlet >
          ????
          < servlet-name > cactusDemo </ servlet-name >
          ????
          < servlet-class > jetty.LoginServlet </ servlet-class >
          ??
          </ servlet >

          ??
          < servlet-mapping >
          ????
          < servlet-name > cactusDemo </ servlet-name >
          ????
          < url-pattern > /cactusDemo </ url-pattern >
          ??
          </ servlet-mapping >
          </ web-app >


          測試 JSP:
          Cookbook 參考?
          不使用容器 進行 測試 jsp

          測試Struts:
          StrutsTestCase 參考?

          主站蜘蛛池模板: 蓝山县| 龙南县| 洪洞县| 团风县| 黄石市| 历史| 长岛县| 英吉沙县| 剑阁县| 铁力市| 道孚县| 凉山| 和平区| 扶余县| 丽水市| 泰兴市| 乌鲁木齐县| 阳城县| 和平区| 扶余县| 神池县| 中宁县| 八宿县| 胶州市| 洪湖市| 休宁县| 阳曲县| 禄劝| 桐梓县| 卢湾区| 邻水| 武穴市| 固安县| 廉江市| 乌什县| 大余县| 喜德县| 天津市| 扶余县| 招远市| 吉安县|