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

          日歷

          <2007年11月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          公告

          由于時間問題,
          blog上一些表達都不太好。

          在此我盡量把我參考的原文給大家,
          與大家學習。^_^
          最近在維護www.aygfsteel.com\Skynet 腳本中
          有什么技術問題不會,
          我很愿意和大家討論!
          多交流快成長
          liukaiyi@gmail.com


          文章檔案

          相冊

          搜索

          •  

          最新評論

          測試 WEB

          Posted on 2007-11-02 12:44 G_G 閱讀(1015) 評論(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 參考?

          主站蜘蛛池模板: 前郭尔| 炉霍县| 井研县| 奉贤区| 台东市| 宜兰县| 黎川县| 永仁县| 睢宁县| 新余市| 洛阳市| 雅江县| 莎车县| 昌吉市| 泽普县| 鄂托克前旗| 南丰县| 淅川县| 胶南市| 霍山县| 哈尔滨市| 大竹县| 太湖县| 阳原县| 久治县| 南丹县| 五寨县| 扎囊县| 大冶市| 襄垣县| 孝感市| 康乐县| 广宗县| 柳江县| 宁晋县| 磐安县| 阳东县| 丘北县| 永仁县| 临夏市| 舟曲县|