您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
用StrutsTestCase測試Struts
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/2/18 14:04:15 ] 推薦標(biāo)簽:

1. StrutsTestCase 簡介

StrutsTestCase 是標(biāo)準(zhǔn) Junit TestCase 的一個(gè)擴(kuò)展,為Struts framework提供了方便靈活的測試代碼
StrutsTestCase 用 ActionServlet controller 進(jìn)行測試,我們可測試Action object, mapping, form bean, forwards
StrutsTestCase 提供了 Mock Object 和 Cactus 兩種方法來運(yùn)行Struts ActionServlet,允許在 servlet engine 內(nèi)或外來測試。
MockStrutsTestCase 使用一系列HttpServlet 模仿對象來模擬容器環(huán)境,CactusStrutsTestCase 使用Cactus testing framework在實(shí)際的容器
中測試

2. 看StrutsTestCase如何工作

我們用的例子是驗(yàn)證用戶名和密碼的action,只要接受參數(shù)然后進(jìn)行簡單的邏輯驗(yàn)證行了,所以我們要做的是:
(1)建好目錄,準(zhǔn)備必要的jar 文件,struts-config.xml, 資源文件
(2)action,formbean 的源文件
(3)test 的源文件
(4)編寫ant 文件,進(jìn)行測試
這里我們用MockStrutsTestCase,CactusStrutsTestCase 的用法也是一樣的,只要改一下繼承類行了

先建立目錄結(jié)構(gòu)
strutstest
|-- src
|-- war
|-- |-- WEB-INF
|-- |-- |-- classes
|-- |-- |-- lib
把struts 所需要的jarfile 和 strutstest, junit 放入lib

然后寫struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<!--
This is the Struts configuration file for the "Hello!" sample application
-->

<struts-config>

<!-- ======== Form Bean Definitions =================================== -->
<form-beans>
<form-bean name="LoginForm" type="LoginForm"/>
</form-beans>

<!-- ======== Global Forwards ====================================== -->
<global-forwards>
<forward name="login" path="/login.jsp"/>
</global-forwards>

<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<!-- Say Hello! -->
<action path = "/login"
type = "LoginAction"
name = "LoginForm"
scope = "request"
validate = "true"
input = "/login.jsp"
>
<forward name="success" path="/hello.jsp" />
</action>
</action-mappings>


<!-- ========== Message Resources Definitions =========================== -->

<message-resources parameter="application"/>

</struts-config>

 

資源文件我們這里只寫一個(gè)用于測試的錯(cuò)誤信息行了, application.properties :

error.password.mismatch=password mismatch


寫LoginForm 來封狀用戶名和密碼:

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class LoginForm extends ActionForm {
public String username;
public String password;

public void setUsername( String username ) {
this.username = username;
}
public String getUsername(){
return username;
}
public void setPassword( String password ) {
this.password = password;
}
public String getPassword(){
return password;
}
}

上一頁123下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd