日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

利用httpunit測試servlet

系統 1930 0

傳統的Java WEB應用中,核心技術莫過于Servlet類與JSP網頁,兩者均可以通過HttpUnit程序包完成單元測試。對JSP網頁的測試主要集中在判斷HTTP服務器返回的內容是否符合要求,并且這種測試只能在WEB容器內進行。對于Servlet類的測試,HttpUnit程序包給出了一個非容器內的測試方案,那就是ServletRunner類的使用。

簡單測試

為了測試Servlet類,首先要在ServletRunner中注冊Servlet類,例如:

Java代碼 復制代碼
  1. //?模擬WEB服務器 ??
  2. ServletRunner?sr?=? new ?ServletRunner(); ??
  3. sr.registerServlet(? "hello.html" ,?HelloServlet. class .getName()?);??

?

上文注冊了一個HelloServlet,當程序發出“hello.html”的HTTP請求時,ServletRunner就會調用HelloServlet類予以響應,如:

Java代碼 復制代碼
  1. //?模擬HTTP客戶端 ??
  2. ServletUnitClient?sc?=?sr.newClient(); ??
  3. ??
  4. //?創建請求 ??
  5. WebRequest?request???= ??
  6. ???? new ?GetMethodWebRequest(? "http://localhost/hello.html" ?); ??
  7. ??
  8. //?返回響應 ??
  9. WebResponse?response?=?sc.getResponse(?request?); ??
  10. ??
  11. //?校驗結果 ??
  12. assertEquals( "text/plain" ,?response.getContentType()); ??
  13. assertEquals( "UTF-8" ,?response.getCharacterSet()); ??
  14. assertEquals( "中國" ,?response.getText());??
  

根據上述測試過程,我們的HelloServlet類實現如下:

Java代碼 復制代碼
  1. import ?java.io.IOException; ??
  2. import ?java.io.PrintWriter; ??
  3. ??
  4. import ?javax.servlet.ServletException; ??
  5. import ?javax.servlet.http.HttpServlet; ??
  6. import ?javax.servlet.http.HttpServletRequest; ??
  7. import ?javax.servlet.http.HttpServletResponse; ??
  8. ??
  9. public ? class ?HelloServlet? extends ?HttpServlet?{ ??
  10. ??
  11. ???? @Override ??
  12. ???? protected ? void ?doGet(HttpServletRequest?req,?HttpServletResponse?resp) ??
  13. ???????????? throws ?ServletException,?IOException?{ ??
  14. ??
  15. ????????resp.setContentType( "text/plain" ); ??
  16. ????????resp.setCharacterEncoding( "UTF-8" ); ??
  17. ??
  18. ????????PrintWriter?pw?=?resp.getWriter(); ??
  19. ????????pw.write( "中國" ); ??
  20. ????} ??
  21. }??
  

當然,我們也可以判斷Servlet類操作session的過程是否正確,如:

Java代碼 復制代碼
  1. import ?junit.framework.TestCase; ??
  2. ??
  3. import ?com.meterware.httpunit.GetMethodWebRequest; ??
  4. import ?com.meterware.httpunit.WebRequest; ??
  5. import ?com.meterware.httpunit.WebResponse; ??
  6. import ?com.meterware.servletunit.ServletRunner; ??
  7. import ?com.meterware.servletunit.ServletUnitClient; ??
  8. ??
  9. public ? class ?HelloTest? extends ?TestCase?{ ??
  10. ??
  11. ???? public ? void ?testHelloServlet()? throws ?Exception?{ ??
  12. ??????? ??
  13. ????????ServletRunner?sr?=? new ?ServletRunner(); ??
  14. ????????sr.registerServlet( "hello.html" ,?HelloServlet. class .getName()); ??
  15. ??????? ??
  16. ????????ServletUnitClient?sc?=?sr.newClient(); ??
  17. ??????? ??
  18. ????????WebRequest?request???= ??
  19. ???????????? new ?GetMethodWebRequest(? "http://localhost/hello.html" ?); ??
  20. ????????WebResponse?response?=?sc.getResponse(?request?); ??
  21. ??????? ??
  22. ???????? //?判斷session中的值 ??
  23. ????????assertEquals( "darxin" ,?sc.getSession( false ).getAttribute( "userId" )); ??
  24. ??????? ??
  25. ????????assertEquals( "text/plain" ,?response.getContentType()); ??
  26. ????????assertEquals( "UTF-8" ,?response.getCharacterSet()); ??
  27. ????????assertEquals( "中國" ,?response.getText());??????? ??
  28. ????} ??
  29. }??
  

相應的,我們的Servlet類會做如下改動:

Java代碼 復制代碼
  1. import ?java.io.IOException; ??
  2. import ?java.io.PrintWriter; ??
  3. ??
  4. import ?javax.servlet.ServletException; ??
  5. import ?javax.servlet.http.HttpServlet; ??
  6. import ?javax.servlet.http.HttpServletRequest; ??
  7. import ?javax.servlet.http.HttpServletResponse; ??
  8. ??
  9. public ? class ?HelloServlet? extends ?HttpServlet?{ ??
  10. ??
  11. ???? @Override ??
  12. ???? protected ? void ?doGet(HttpServletRequest?req,?HttpServletResponse?resp) ??
  13. ???????????? throws ?ServletException,?IOException?{ ??
  14. ??
  15. ???????? //?向session中設置屬性 ??
  16. ????????req.getSession().setAttribute( "userId" ,? "darxin" ); ??
  17. ??????? ??
  18. ????????resp.setContentType( "text/plain" ); ??
  19. ????????resp.setCharacterEncoding( "UTF-8" ); ??
  20. ??
  21. ????????PrintWriter?pw?=?resp.getWriter(); ??
  22. ????????pw.write( "中國" ); ??
  23. ????} ??
  24. }??
  

高級應用

上述兩例均屬于在Servlet類中直接打印響應信息的情況,在實際應用中這種調用已經很少見了。通常我們會利用MVC架構實現Servlet類與JSP網頁的功能分離。例如使用Servlet類完成Controller的任務;使用JSP網頁完成View的任務。
下圖展示了一個典型的利用MVC架構實現HTTP響應的過程:




根據這個圖可以看出,第五步會在Servlet類用到轉向操作,轉向操作的方法如下例:

Java代碼 復制代碼
  1. //?轉向到新的servlet ??
  2. request.getRequestDispatcher( "main.html" ).forward(request,?response);??????
  

?

如何測試具有轉向功能的Servlet類呢?首先要明確對于這一類Servlet,我們要測試它們的什么功能:
第一, Servlet類在轉向前都保存了哪些數據?保存這些數據的位置在哪兒?
第二, Servlet類是否轉向到正確的位置上了?

需要注意的是,通常情況下作為Controller的Servlet類是要轉向到作為View的JSP網頁的,但是HttpUnit程序包不提供解析JSP網頁的方法。為此,我們可以利用stub技術,利用另一個Servlet類為其模擬一個轉向目標。
模擬轉向目標的任務有兩個:
第一, 從數據保存區提取相關的數據;
第二, 將相關的數據以響應的方式向用戶端發送。
作為stub的Servlet類不需要進行數據的有效性判斷。樣例代碼如下:

Java代碼 復制代碼
  1. import ?java.io.IOException; ??
  2. import ?java.io.PrintWriter; ??
  3. ??
  4. import ?javax.servlet.ServletException; ??
  5. import ?javax.servlet.http.HttpServlet; ??
  6. import ?javax.servlet.http.HttpServletRequest; ??
  7. import ?javax.servlet.http.HttpServletResponse; ??
  8. ??
  9. public ? class ?MainStub? extends ?HttpServlet?{ ??
  10. ??
  11. ???? @Override ??
  12. ???? protected ? void ?doGet(HttpServletRequest?req,?HttpServletResponse?resp) ??
  13. ???????????? throws ?ServletException,?IOException?{ ??
  14. ??????? ??
  15. ???? //?從數據保存區提取相關的數據 ??
  16. ????????String?userId?=?(String)req.getAttribute( "userId" ); ??
  17. ??????? ??
  18. ????????resp.setContentType( "text/plain" ); ??
  19. ????????resp.setCharacterEncoding( "UTF-8" ); ??
  20. ??
  21. ???????? //?將相關的數據以響應的方式向用戶端發送 ??
  22. ????????PrintWriter?pw?=?resp.getWriter(); ??
  23. ????????pw.write(userId); ??
  24. ????} ??
  25. }??
  

相應的,用戶端測試代碼的任務是:
第一, 注冊需要測試的Servlet類與用作stub的Servlet類;
第二, 模擬調用需要測試的Servlet類并為其提供參數;
第三, 檢查從用作stub的Servlet類中返回的響應數據是否符合要求。
樣例代碼如下:

Java代碼 復制代碼
  1. import ?junit.framework.TestCase; ??
  2. ??
  3. import ?com.meterware.httpunit.GetMethodWebRequest; ??
  4. import ?com.meterware.httpunit.WebRequest; ??
  5. import ?com.meterware.httpunit.WebResponse; ??
  6. import ?com.meterware.servletunit.ServletRunner; ??
  7. import ?com.meterware.servletunit.ServletUnitClient; ??
  8. ??
  9. public ? class ?HelloTest? extends ?TestCase?{ ??
  10. ??
  11. ???? public ? void ?testHelloServlet()? throws ?Exception?{ ??
  12. ??
  13. ????????ServletRunner?sr?=? new ?ServletRunner(); ??
  14. ???????? //?注冊測試用Servlet??????? ??
  15. ????????sr.registerServlet( "hello.html" ,?HelloServlet. class .getName()); ??
  16. ???????? //?注冊stub用Servlet??????? ??
  17. ????????sr.registerServlet( "main.html" ,?MainStub. class .getName()); ??
  18. ??????? ??
  19. ????????ServletUnitClient?sc?=?sr.newClient(); ??
  20. ??????? ??
  21. ???????? //?調用測試用Servlet并為其提供參數 ??
  22. ????????WebRequest?request???= ??
  23. ???????????? new ?GetMethodWebRequest(? "http://localhost/hello.html?userId=darxin" ?); ??
  24. ????????WebResponse?response?=?sc.getResponse(?request?); ??
  25. ??
  26. ???????? //?檢查最終的返回結果??????? ??
  27. ????????assertEquals( "darxin" ,?response.getText());??????? ??
  28. ????} ??
  29. }??
  

根據測試代碼及stub代碼,我們最終需要完成的Servlet類代碼如下:

Java代碼 復制代碼
  1. import ?java.io.IOException; ??
  2. import ?javax.servlet.ServletException; ??
  3. ??
  4. import ?javax.servlet.http.HttpServlet; ??
  5. import ?javax.servlet.http.HttpServletRequest; ??
  6. import ?javax.servlet.http.HttpServletResponse; ??
  7. ??
  8. public ? class ?HelloServlet? extends ?HttpServlet?{ ??
  9. ??
  10. ???? @Override ??
  11. ???? protected ? void ?doGet(HttpServletRequest?req,?HttpServletResponse?resp) ??
  12. ???????????? throws ?ServletException,?IOException?{ ??
  13. ??
  14. ???????? //?從請求中取出參數 ??
  15. ????????String?userId?=?req.getParameter( "userId" ); ??
  16. ??
  17. ???????? //?向request中設置屬性 ??
  18. ????????req.setAttribute( "userId" ,?userId); ??
  19. ??
  20. ???????? //?轉向到新的servlet ??
  21. ????????req.getRequestDispatcher( "main.html" ).forward(req,?resp);??????? ??
  22. ????} ??
  23. }??
  

?

以上簡要說明了如何利用HttpUnit程序包測試Servlet的方法,此方法適用于基本的Servlet實現。
對于容器內測試,建議使用Cactus技術。

?

利用httpunit測試servlet


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 五原县| 昌邑市| 玉屏| 宣威市| 新巴尔虎右旗| 探索| 岳普湖县| 东乌珠穆沁旗| 德兴市| 余姚市| 油尖旺区| 富顺县| 定南县| 乡宁县| 和静县| 新蔡县| 鹿泉市| 内黄县| 赞皇县| 汉寿县| 和静县| 云霄县| 永善县| 怀化市| 潢川县| 定襄县| 万山特区| 堆龙德庆县| 稻城县| 手游| 大名县| 马鞍山市| 柳林县| 久治县| 宿迁市| 莱芜市| 姜堰市| 巴林左旗| 金堂县| 苏州市| 克什克腾旗|