下载http://sourceforge.net/projects/strutstestcase/
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
-
sources
/>
<
form
-
beans
>
<
form
-
bean name
=
"
loginForm
"
type
=
"
com.yourcompany.struts.form.LoginForm
"
/>
</
form
-
beans
>
<
global
-
exceptions
/>
<
global
-
forwards
/>
<
action
-
mappings
>
<
action
name
=
"
loginForm
"
path
=
"
/login
"
scope
=
"
request
"
type
=
"
com.yourcompany.struts.action.LoginAction
"
>
<
forward
name
=
"
ok
"
path
=
"
/form/suss.jsp
"
/>
<
forward
name
=
"
error
"
path
=
"
/form/login.jsp
"
/>
</
action
>
</
action
-
mappings
>
<
message
-
resources parameter
=
"
com.yourcompany.struts.ApplicationResources
"
/>
</
struts
-
config
>
Action
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.yourcompany.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.yourcompany.struts.form.LoginForm;
/**
* MyEclipse Struts
* Creation date: 04-16-2007
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
* @struts.action-forward name="Myjsp" path="/WEB-INF/"
*/
public class LoginAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
String name = loginForm.getName() ;
String pass = loginForm.getPass() ;
if(name.equals(pass)){
return mapping.findForward("login");
}
return mapping.findForward("ok");
}
}
Form
package com.yourcompany.struts.form;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
private String name ;
private String pass ;
public void setName(String name){
this.name = name ;
}
public String getName(){
return this.name ;
}
public void setPass(String pass){
this.pass = pass ;
}
public String getPass(){
return this.pass ;
}
}
关键的来了 ----
package test;
import java.io.File;
import servletunit.struts.MockStrutsTestCase;
import junit.framework.TestCase;
//
public class StrutsTest extends MockStrutsTestCase {
public StrutsTest(String testName){
super(testName);
}
public void setUp()throws Exception{
super.setUp();
File web = new File("E:/src/StrutsTestCase/WebRoot/");
this.setContextDirectory(web);
setConfigFile("E:/src/StrutsTestCase/WebRoot/WEB-INF/web.xml");
setConfigFile("E:/src/StrutsTestCase/WebRoot/WEB-INF/struts-config.xml");
}
public void testAction() {
setRequestPathInfo("/login");
this.addRequestParameter("name","liu");
this.addRequestParameter("pass","123");
actionPerform();
this.verifyForward("ok");
}
}
不需要 写 classpath 哈哈