欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

利用httpunit測(cè)試servlet

系統(tǒng) 1855 0

傳統(tǒng)的Java WEB應(yīng)用中,核心技術(shù)莫過(guò)于Servlet類(lèi)與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類(lèi)的測(cè)試,HttpUnit程序包給出了一個(gè)非容器內(nèi)的測(cè)試方案,那就是ServletRunner類(lèi)的使用。

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

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

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




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

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

?

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

需要注意的是,通常情況下作為Controller的Servlet類(lèi)是要轉(zhuǎn)向到作為View的JSP網(wǎng)頁(yè)的,但是HttpUnit程序包不提供解析JSP網(wǎng)頁(yè)的方法。為此,我們可以利用stub技術(shù),利用另一個(gè)Servlet類(lèi)為其模擬一個(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類(lèi)不需要進(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類(lèi)與用作stub的Servlet類(lèi);
第二, 模擬調(diào)用需要測(cè)試的Servlet類(lèi)并為其提供參數(shù);
第三, 檢查從用作stub的Servlet類(lèi)中返回的響應(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類(lèi)代碼如下:

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

您的支持是博主寫(xiě)作最大的動(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ì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 色综合天天综合网国产成人 | 狠狠色噜噜综合社区 | 成人一区二区三区在线观看 | 91av在线电影| 国内精品视频在线观看 | 国产亚洲精品一品区99热 | 色综合中文字幕 | 亚洲午夜精品视频 | 99久久精品免费看国产免费 | 日本高清成人 | 国产精品91在线播放 | 国产乱色精品成人免费视频 | 亚洲日本中文字幕在线2022 | 达达兔午夜起神影院在线观看麻烦 | 欧美日韩国产欧美 | 麻豆视频在线播放 | 精品久久久久久亚洲 | 久久亚洲日本不卡一区二区 | 在线观看亚洲一区二区三区 | 欧美性色生活片免费播放 | 国产激情 | 国产一区在线免费观看 | 91色在线 | ak福利视频 | 一本伊大人香蕉久久网手机 | 人人人人人爽 | 天天摸天天爽天天澡视频 | 亚洲va欧美va天堂v国产综合 | 97国产精品视频人人做人人爱 | 狠日日 | 人人爱免费在线观看 | 国产亚洲精品高清在线 | 精品在线91 | 久久久久国产成人精品亚洲午夜 | 天天操夜夜做 | av免费在线免费观看 | 黄色av毛片 | 欧美a在线 | 麻豆污视频 | 欧美激情精品久久久久 | 浮力影院网站午夜 |