Posted on 2007-11-02 12:44
G_G 閱讀(1011)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
JUnit
學(xué)習(xí)測(cè)試留個(gè)印
測(cè)試 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(
"
直接獲取網(wǎng)頁(yè)內(nèi)容:
"
);
????????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(
"
無(wú)參數(shù)測(cè)試:
"
);
????????
//
建立服務(wù)器
????????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?);
????????
//
運(yùn)行
????????System.out.println(response.getText()
+
"
\n
"
);
????}
????
????
public
?
void
?testRun2()
throws
?Exception{
????????System.out.println(
"
Session?和?request?測(cè)試:
"
);
????????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?方法準(zhǔn)備執(zhí)行
????????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());
//
關(guān)鍵:運(yùn)行測(cè)試方法
????????
????????
//
獲取?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
>
測(cè)試 JSP:
Cookbook
參考?
不使用容器 進(jìn)行 測(cè)試 jsp
測(cè)試Struts:
StrutsTestCase
參考?