Struts
測(cè)試之
action
測(cè)試
???
測(cè)試
Struts Action
相對(duì)比較困難
,
因?yàn)?/font>
Struts
是運(yùn)行在
Web
服務(wù)器中
,
因此要測(cè)試
Struts Action
就必須發(fā)布應(yīng)用程序然后才能測(cè)試,換言之,我們必須要有
Web
容器的支持
.
我們想象一下
,
對(duì)于一個(gè)擁有上千個(gè)
JSP page
和數(shù)百甚至數(shù)千
Java Classes
的大規(guī)模應(yīng)用程序
,
要把他們發(fā)布到諸如
Weblogic
之類(lèi)的應(yīng)用服務(wù)器再測(cè)試
,
需要多少的時(shí)間和硬件資源
?
所以這種模式的測(cè)試是非常費(fèi)時(shí)費(fèi)力的
.
所以
,
如果有一種辦法能夠不用發(fā)布應(yīng)用程序
,
不需要
Web
服務(wù)器就能象測(cè)試普通
Java Class
一樣測(cè)試
Struts Action,
那就能極大地加強(qiáng)
Struts
的可測(cè)試性能
,
使應(yīng)用程序測(cè)試更為容易
,
簡(jiǎn)單快速
.
現(xiàn)在這個(gè)工具來(lái)了
,
這就是
StrutsTestCase.
StrutsTestCase
是一個(gè)開(kāi)源工具
,
可以到
http://strutstestcase.sourceforge.net
下載
.
目前最新版本是
2.1.3,
如果你使用
Servlet2.3
就下載
StrutsTestCase213-2.3.jar,
使用
Servlet2.4
的就下載
StrutsTestCase213-2.4.jar.
另外
StrutsTestCase
本身就是從
JUnit
繼承的
,
所以你還需要下載
JUnit3.8.1.
?????
下面就以一個(gè)簡(jiǎn)單的
LogonAction
為例測(cè)試一下:
public class LogonAction extends Action {
????? /**
?????
?* Method execute
?????
?* @param mapping
?????
?* @param form
?????
?* @param request
?????
?* @param response
?????
?* @return ActionForward
?????
?*/
?????
?????
????? public ActionForward execute(
?????????? ActionMapping mapping,
?????????? ActionForm form,
?????????? HttpServletRequest request,
?????????? HttpServletResponse response) {
?
?????????? DynaValidatorForm dynaForm = (DynaValidatorForm)form;
?
?????????? String name = (String)dynaForm.get("username");
?????????? String password = (String)dynaForm.get("password");
??????????
?????????? if (name.equals("wangxq") && password.equals("wangxq")){
???????????????? request.setAttribute("valid_user",form);
???????????????? return mapping.findForward("admin");
?????????? }
?????????? return mapping.findForward("success");
????? }
?
}
??????
LogonAction
的簡(jiǎn)單說(shuō)明:從
Logon
的頁(yè)面中輸入用戶(hù)名和密碼,在
LogonAction
中作判斷,并且作相應(yīng)的跳轉(zhuǎn)。
?????
對(duì)其的測(cè)試代碼如下:
????? public class LogonActionTest extends MockStrutsTestCase {
?
????? protected void setUp() throws Exception {
?????????? super.setUp();
?????????? setContextDirectory(new File("WebRoot")); //
設(shè)置
WEB-INF
的上級(jí)目錄,讓程序可以找到
struts-config.xml
文件
?????????? }
?
????? protected void tearDown() throws Exception {
?????????? super.tearDown();
????? }
?????
????? public void testNoParameters(){
??????????
?????????? setRequestPathInfo("/logon");
?????????? actionPerform();
?????????? verifyInputForward();
?????????? String[] actionErrors = {"errors.required","errors.required"};
?????????? verifyActionErrors(actionErrors);
?????????? verifyInputForward();
????? }
?????
????? public void testOneParameters(){
?????????? setRequestPathInfo("/logon");
?????????? addRequestParameter("username","wangxq");
?????????? actionPerform();
?????????? //
校驗(yàn)
Action
是否轉(zhuǎn)發(fā)到
Action Mapping
里的
input
屬性
?????????? verifyInputForward();
?????????? String[] actionErrors ={"errors.required"};
?????????? verifyActionErrors(actionErrors);
?????????? verifyInputForward();
????? }
?????
????? public void testSuccessAdmin(){
?????????? //
設(shè)置
Request
的請(qǐng)求,說(shuō)明該
Request
請(qǐng)求的是哪一個(gè)
Action
,或者說(shuō),請(qǐng)求的是哪一個(gè)
.do
文件。
?????????? setRequestPathInfo("/logon");
?????????? //
將參數(shù)和其對(duì)應(yīng)的值加入到
request
中,相當(dāng)于是
action
對(duì)應(yīng)的
formbean
傳過(guò)來(lái)的值,即用戶(hù)在登陸界面輸入的值。
?????????? addRequestParameter("username","wangxq");
?????????? addRequestParameter("password","wangxq");
?????????? //
執(zhí)行這個(gè)請(qǐng)求,即執(zhí)行
action
中對(duì)應(yīng)的
execute
方法。
?????????? actionPerform();
?????????? //
驗(yàn)證
forward
的名字是否正確,即有沒(méi)有跳轉(zhuǎn)到預(yù)期的頁(yè)面。
?????????? verifyForward("admin");
//
驗(yàn)證沒(méi)有任何的
ActionErrors
。
?????????? verifyNoActionErrors();
????? }
?
????? public void testSuccessLogon(){
?????????? setRequestPathInfo("/logon");
?????????? addRequestParameter("username","aaaaaa");
?????????? addRequestParameter("password","bbbbbb");
?????????? actionPerform();
?????????? verifyForward("success");
?????????? verifyNoActionErrors();
????? }
}
?
補(bǔ)充說(shuō)明其中的一些方法:
verifyActionErrors/Messages --
校驗(yàn)
ActionActionServlet controller
是否發(fā)送了
ActionError
或
ActionMessage.
參數(shù)為
ActionError/Message Key
verifyNoActionErrors/Messages --
校驗(yàn)
ActionActionServlet controller
沒(méi)有發(fā)送
ActionError
或
ActionMessage
VerifyForward --
校驗(yàn)
Action
是否正確轉(zhuǎn)發(fā)到指定的
ActionForward.
VerifyForwardPath --
校驗(yàn)
Action
是否正確轉(zhuǎn)發(fā)到指定的
URL
verifyInputForward --
校驗(yàn)
Action
是否轉(zhuǎn)發(fā)到
Action Mapping
里的
input
屬性
其他的方法可以參考具體的文檔說(shuō)明。
?
還有一點(diǎn)需要說(shuō)明:
關(guān)于Web.xml和Struts-Config.xml
缺省情況下
,StrutsTestCase
認(rèn)為你的
Web.xml
和
struts-config.xml
的路徑分別是
:
/WEB-INF/web.xml
和
/WEB-INF/struts-config.xml
1.
假如你的
web.xml/struts-config.xml
的路徑是
d:/application/web/WEB-INF/web.xml(struts-config.xml)
的話(huà)
,
就需要把
d:/ application /web
加到
classpath.
?????
或者更簡(jiǎn)單的方法是
setContextDirectory(new File("web"))
這樣就可以找到了。
2.
假如你的
struts config
是
strust-config-module.xml,
那么必須調(diào)用
setConfigFile()
設(shè)置你的
struts config
文件
?
深入使用:
?????????? <action path="/handle"
????????????????????? input="/handle.do?method=setUp"
?
?????????????????? name=" handleForm"
?
?????????????????? type=" handleAction"
?
?????????????????? scope="session"
?
?????????????????? parameter="method"
?
?????????????????? validate="true">
?
????????????? <forward name="CurPage" path="
handle "/>
?
??????? </action>
這段配置文件中,使用了
parameter="method"
的配置,這樣在測(cè)試的時(shí)候就需要設(shè)置以下:
測(cè)試代碼中應(yīng)該加入:
addRequestParameter("method ","setUp");
這樣,在執(zhí)行
actionPerform()
時(shí),程序就自動(dòng)進(jìn)入
setUp
的方法,執(zhí)行該方法的測(cè)試。
?