Spring和Hibernate利用JUnit进行测试,大家已经很熟悉.
Struts则利用StrutsTest测试.
下面我们测试一个登录的Struts程序
LoginForm.java 文件
package com.bcxy.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginForm extends ActionForm {
private String user;
private String pass;
/**
* Returns the user.
* @return String
*/
public String getUser() {
return user;
}
/**
* Set the user.
* @param user The user to set
*/
public void setUser(String user) {
this.user = user;
}
/**
* Returns the pass.
* @return String
*/
public String getPass() {
return pass;
}
/**
* Set the pass.
* @param pass The pass to set
*/
public void setPass(String pass) {
this.pass = pass;
}
}
LoginAction.java 文件
package com.bcxy.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.bcxy.struts.form.LoginForm;
public class LoginAction extends Action {
/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
LoginForm loginForm = (LoginForm) form;
String user = loginForm.getUser();
String pass = loginForm.getPass();
if(user.equals(pass)){
return mapping.findForward("success");
}else{
return mapping.findForward("fail");
}
}
}
struts-config.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- ========== Data Source Configuration =============================== -->
<data-sources />
<!-- ========== Form Bean Definitions ================================== -->
<form-beans>
<form-bean name="loginForm" type="com.bcxy.struts.form.LoginForm">
<form-property name="user" type="java.lang.String" />
<form-property name="pass" type="java.lang.String" />
</form-bean>
</form-beans>
<!-- ========== Global Exception Definitions ============================== -->
<global-exceptions />
<!-- ========== Global Forward Definitions =============================== -->
<global-forwards />
<!-- ========== Action Mapping Definitions =============================== -->
<action-mappings>
<action
type="com.bcxy.struts.action.LoginAction"
name="loginForm"
path="/login"
scope="request"
validate="false">
<forward name="success" path="/success.jsp"/>
<forward name="fail" path="/fail.jsp"/>
</action>
</action-mappings>
<!-- ========== Controller Configuration ================================ -->
<controller />
<!-- ========== Message Resources Definitions ============================ -->
<!-- ========== Plug Ins Configuration ================================= -->
<message-resources parameter="com.bcxy.struts.ApplicationResources" />
</struts-config>
下面我们写一个测试文件,它继承strtustest的MockStrutsTestCase类.
/*
* Created on 2004-10-31
*
*/
package test;
import java.io.File;
import servletunit.struts.MockStrutsTestCase;
/**
* @author ezerg
*/
public class LoginTest extends MockStrutsTestCase {
public LoginTest() {
super();
}
/**
* @param arg0
*/
public LoginTest(String arg0) {
super(arg0);
}
public void setUp() throws Exception{
super.setUp();
//指明web应用的根
File context = new File("defaultroot");
setContextDirectory(context);
}
public void tearDown() throws Exception{
super.tearDown();
}
public void testLogin(){
//设置action的path
setRequestPathInfo("/login.do");
//准备所需要的参数
addRequestParameter("user", "test");
addRequestParameter("pass", "test");
//执行
actionPerform();
//验证返回的forward
verifyForward("success");
}
}
在Eclipse中执行run as JUnit Test可看到结果....
主要函数和作用:
- setContextDirectory,设置web应用的根
- setRequestPathInfo,设置request的请求
- addRequestParameter,将参数和对应的值加入request中
- actionPerform,执行这个请求
- verifyForward,验证forward的名字是否正确
- verifyForwardPath,验证forward的path是否正确
- verifyNoActionErrors,验证在action执行过程中没有ActionError产生
- verifyActionErrors,验证在action执行过程中产生的ActionError集合的内容
测试Tile。其基本过程与上面类似,不同之处在于验证:使用verifyTilesForward和verifyInputTilesForward。
测试子模块。其基本过程也和上面一样,不同之处在于需要指定该模块所需要使用的struts-config.xml和调用方式:
- 首先,指定所需要的struts-config.xml:
setConfigFile("mymodule","/WEB-INF/struts-config-mymodule.xml");
- 其次,指明调用的路径:
setRequestPathInfo("/mymodule","/login.do");
使用StrutsTest需要注意的地方:
- 在TestCase中可以访问request、session等对象,StrutsTest为我们提供了访问接口。
- junit的assert函数和fail函数都可以在StrutsTest中使用。
- 对于文件上载,目前StrutsTest并没有什么特别好的方案。具体参见StrusTest的Faq。
下载地址:http://sourceforge.net/projects/strutstestcase/
如果有什么问题,请与我联系:[email protected]
ezerg 编程小语
本文地址:http://com.8s8s.com/it/it14013.htm