Struts
Be currently in
Web
One of several frames widely used in the development, and
Strutstest
It is specifically responsible for testing
Strut
Application
Mock
Test frame.
purpose of usage
Strtustest is an extension of JUnit. It is easy to test the Struts application (container external test) without starting a servlet container. It also belongs to the use of the Mock object test, but it is different from Easymock that Easymock is an API that provides a Mock object, and StrutStest is dedicated to test the MOCK object test framework for testing the Struts application. In addition to the container test, StrutStest can also be very convenient to test the test in the container.
The current version is strutstest2.1.0, and Struts 1.0 is not supported in this release. If you need to test the Struts1.0 application, the corresponding version is Strutstest2.0. You can download from http://sourceforge.net/projects/strutstestcase/.
usage
1. Basic steps:
- Writing struts anctions.
- Determine how to test: Mock objects choose MockStrutStestCase as base classes; CACTUSTRUTSTESTSTCASE; these two modes are the same.
- Use Setup and Teardown to initialize, clarify the first sentence of these two functions to call the SUPER method.
- Set the path to the Struts Action to be tested, so that strutste can find Web.xml and Struts-related configuration documentation.
- Set the Action corresponding to the corresponding PATH name in Strtus-Config.xml.
- Set the various parameter values to the Request object to be used in the Action, including the value of the FORMBEAN used by the action.
- Execute an action.
- Verify that the return path of the action is correct, that is, the name of Forward.
- Verify the rest of the Action.
2. Use example (using a MOCK mode):
Public class deparmentdbactiontest extends mockstrutstestcase {
Public DeparmentdbActionTest (String arg0) {
Super (arg0);
}
Public void setup () {
Super.setup ();
/ / Indicate the root of the web application
File contextfile = new file ("D: // projects // fog // impLement // Web");
SetContextDirectory (contextfile);
}
protected void teardown () throws exception {
Super.teardown ();
}
Public void testdoadd () {
// Set the Action's path
SetRequestPathInfo ("/ DPEDIT");
// Prepare the parameters of the FormBean required for Action
AddRequestParameter ("Method", "DoAdd");
AddRequestParameter ("PageID", "1");
AddRequestParameter ("DPNAME", "Test Department");
AddRequestParameter ("DPTYPE", "Test Department"); AddRequestParameter ("YN", "N");
// Execute an action
ActionPerform ();
// Verify the return forward
VerifyForward ("Success");
}
3. 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
For details, please refer to the corresponding javadoc.
4. Test Tile. Its basic procedure is similar to the above, and the difference is that verification: uses VerifyTilesForward and VerifyInputTilesForward.
5. Test the child module. 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");
checklist
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.