亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

利用httpunit測(cè)試servlet

系統(tǒng) 2020 0

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

簡(jiǎn)單測(cè)試

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

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

?

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

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

根據(jù)上述測(cè)試過(guò)程,我們的HelloServlet類實(shí)現(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( "中國(guó)" ); ??
  20. ????} ??
  21. }??
  

當(dāng)然,我們也可以判斷Servlet類操作session的過(guò)程是否正確,如:

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( "中國(guó)" ,?response.getText());??????? ??
  28. ????} ??
  29. }??
  

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

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( "中國(guó)" ); ??
  23. ????} ??
  24. }??
  

高級(jí)應(yīng)用

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




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

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

?

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

需要注意的是,通常情況下作為Controller的Servlet類是要轉(zhuǎn)向到作為View的JSP網(wǎng)頁(yè)的,但是HttpUnit程序包不提供解析JSP網(wǎng)頁(yè)的方法。為此,我們可以利用stub技術(shù),利用另一個(gè)Servlet類為其模擬一個(gè)轉(zhuǎn)向目標(biāo)。
模擬轉(zhuǎn)向目標(biāo)的任務(wù)有兩個(gè):
第一, 從數(shù)據(jù)保存區(qū)提取相關(guān)的數(shù)據(jù);
第二, 將相關(guān)的數(shù)據(jù)以響應(yīng)的方式向用戶端發(fā)送。
作為stub的Servlet類不需要進(jìn)行數(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)的,用戶端測(cè)試代碼的任務(wù)是:
第一, 注冊(cè)需要測(cè)試的Servlet類與用作stub的Servlet類;
第二, 模擬調(diào)用需要測(cè)試的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. ???????? //?注冊(cè)測(cè)試用Servlet??????? ??
  15. ????????sr.registerServlet( "hello.html" ,?HelloServlet. class .getName()); ??
  16. ???????? //?注冊(cè)stub用Servlet??????? ??
  17. ????????sr.registerServlet( "main.html" ,?MainStub. class .getName()); ??
  18. ??????? ??
  19. ????????ServletUnitClient?sc?=?sr.newClient(); ??
  20. ??????? ??
  21. ???????? //?調(diào)用測(cè)試用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ù)測(cè)試代碼及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. ???????? //?從請(qǐng)求中取出參數(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. }??
  

?

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

?

利用httpunit測(cè)試servlet


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 欧美洲久久日韩欧美 | 国产1区2区三区不卡 | 黄色免费网站在线观看 | 日本自己的私人影院 | 五月天婷婷在线免费观看 | 日韩成人在线网站 | 黄毛片| 亚洲一区视频在线 | 亚洲精品乱码国产精品乱码 | 精品小视频在线观看 | 亚洲欧美日韩专区一 | 波多野结衣中文一区二区免费 | 亚洲七七久久综合桃花 | 日韩最新视频一区二区三 | 97在线亚洲| 欧美日韩国产欧美 | 免费视频网站在线观看黄 | 久久久久久99精品 | 在线观看日本免费视频大片一区 | 97在线观看免费观看高清 | 涩涩视频免费观看 | 午夜伦y4480影院中文字幕 | 赛车总动员2在线观看 | 4虎影院午夜在线观看 | 日本不卡免免费观看 | 久久夜色精品国产噜噜小说 | 男女拍拍视频黄的全免费 | 高清国产天干天干天干不卡顿 | 美女被爆羞羞网站 | 国产精品久久久亚洲 | 激情久久久久久久久久 | 欧洲自拍偷拍 | 欧美a在线 | 999精品国产| 免费a级毛片大学生免费观看 | 久草小区二区三区四区网页 | 免费看欧美理论片在线 | 2020久久精品国产免费 | se999se男人最爱| 日本精品高清一区二区不卡 | 国产区综合另类亚洲欧美 |