黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

利用httpunit測試servlet

系統(tǒng) 2194 0

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

簡單測試

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

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

?

上文注冊了一個HelloServlet,當程序發(fā)出“hello.html”的HTTP請求時,ServletRunner就會調(diào)用HelloServlet類予以響應(yīng),如:

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

根據(jù)上述測試過程,我們的HelloServlet類實現(xiàn)如下:

Java代碼 復(fù)制代碼
  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代碼 復(fù)制代碼
  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. }??
  

相應(yīng)的,我們的Servlet類會做如下改動:

Java代碼 復(fù)制代碼
  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中設(shè)置屬性 ??
  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. }??
  

高級應(yīng)用

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




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

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

?

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

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

Java代碼 復(fù)制代碼
  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. ???? //?從數(shù)據(jù)保存區(qū)提取相關(guān)的數(shù)據(jù) ??
  16. ????????String?userId?=?(String)req.getAttribute( "userId" ); ??
  17. ??????? ??
  18. ????????resp.setContentType( "text/plain" ); ??
  19. ????????resp.setCharacterEncoding( "UTF-8" ); ??
  20. ??
  21. ???????? //?將相關(guān)的數(shù)據(jù)以響應(yīng)的方式向用戶端發(fā)送 ??
  22. ????????PrintWriter?pw?=?resp.getWriter(); ??
  23. ????????pw.write(userId); ??
  24. ????} ??
  25. }??
  

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

Java代碼 復(fù)制代碼
  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. ???????? //?調(diào)用測試用Servlet并為其提供參數(shù) ??
  22. ????????WebRequest?request???= ??
  23. ???????????? new ?GetMethodWebRequest(? "http://localhost/hello.html?userId=darxin" ?); ??
  24. ????????WebResponse?response?=?sc.getResponse(?request?); ??
  25. ??
  26. ???????? //?檢查最終的返回結(jié)果??????? ??
  27. ????????assertEquals( "darxin" ,?response.getText());??????? ??
  28. ????} ??
  29. }??
  

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

Java代碼 復(fù)制代碼
  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. ???????? //?從請求中取出參數(shù) ??
  15. ????????String?userId?=?req.getParameter( "userId" ); ??
  16. ??
  17. ???????? //?向request中設(shè)置屬性 ??
  18. ????????req.setAttribute( "userId" ,?userId); ??
  19. ??
  20. ???????? //?轉(zhuǎn)向到新的servlet ??
  21. ????????req.getRequestDispatcher( "main.html" ).forward(req,?resp);??????? ??
  22. ????} ??
  23. }??
  

?

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

?

利用httpunit測試servlet


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論