Spring and Hibernate use JUnit to test, everyone is already familiar.
Struts uses the Strutstest test.
Below we test a STRUTS program
LOGINFORM.JAVA file
Package com.bcxy.struts.form;
Import javax.servlet.http.httpservletRequest;
Import org.apache.struts.Action.ActionerroTs.Act Org.Apache.struts.Action.Apache.struts.Action.Apache.Struts.Action.Apache.Struts.Action.ApsMapping;
Public class loginform extends an 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 file
Package com.bcxy.struts.Art;
Import javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse;
Import org.Apache.struts.Action.Act; import org.apache.struts.action.actionform; import org.apache.struts.action.actionforward; import org.apache.struts.Action.Apache.APPING;
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 file
XML Version = "1.0" Encoding = "UTF-8"?>
/ * * 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 (); // Indicate the root of the web application File CONTEXT = New File (" defaultroot "); setContextDirectory (context);} public void tearDown () throws Exception {super.tearDown ();} public void testLogin () {// set the action path setRequestPathInfo (" / login.do "); // prepare The required parameters addRequestParameter ("User", "Test"); addRequestParameter ("pass", "test"); // execute actionPerform (); // verify the returned Forward VerifyForward ("Success");}
}
Executive Run As Junit Test in Eclipse can see the result ....
Main functions and roles:
- setContextDirectory, set the root of web applications
- setRequestPathInfo, set request request
- AddRequestParameter Add the parameters and the corresponding value to the request
- ActionPerform, execute this request
- VerifyForward, verify that the name of Forward is correct
- VerifyForwardPath, verify that the forward Path is correct
- VerifyNoActionerrorS, verify that there is no ActionError during action execution
- VerifyActionerRRORS, verify the contents of the ActionError collection generated during action execution
Test Tile. Its basic procedure is similar to the above, and the difference is that verification: uses VerifyTilesForward and VerifyInputTilesForward.
The test submodule. The basic process is also the same as above, and the difference is that the Struts-Config.xml and call mode required to specify the module:
- First, specify the Struts-Config.xml required:
SetConfigfile ("MyModule", "/ Web-INF / STRUTS-Config-mymodule.xml");
- Second, specify the path to the call:
SetRequestPathInfo ("/ mymodule", "/ login.do");
Places you need to pay attention to using Strutstest:
- You can access the request, session and other objects in TestCase, and StrutStest provides us with access interface.
- JUnit's Assert function and FAIL function can be used in Strutstest. - For file upload, the current StrutStest doesn't have any particularly good solution. See the Strustest's FAQ.
Download address: http://sourceforge.net/projects/strutstestcase/
If there is any problem, please contact me: webmaster@bcxy.com
Ezerg Programming