We often ignore the unit tests that show layer action during the development of Web applications, mainly because:
1, the action layer of the business logic is relatively simple. It is the subconscious mind that this part of the code is unimportant.
2, the action layer is difficult to simulate the HTTP request pass parameters, the need to rely on the Web container, so the unit test writing to bring some difficulty.
I wrote a simple action unit test case for your reference. Both a mock and a webwork actionproxyfactory based on struts can perform action unit tests. I personally prefer to do unit tests with actionproxyfactory. In fact, the Write Action unit test is very simple, roughly divided into three steps to complete the unit test:
Setting Actioncontext Context Parameters
To add the request parameters passed by the form to the map
Create action Dynamic Proxy object
Via public abstract Actionproxy Createactionproxy (string namespace, String actionname, Map extracontext) throws Exception Build the action proxy object.
Iii. JUnit Assertion Execution Results
Assertequals (Testaction.login (), "success")
Detailed use case reference:
public class Testactiontest extends basecasetest{private actionproxy proxy = null;
Private Ivoucherservice Voucherservice;
@Before public void SetUp () throws Exception {Imockscontrol control = Easymock.createcontrol ();
Voucherservice = Control.createmock (Ivoucherservice.class);
map<string, object> params = new hashmap<string, object> ();
Params.put ("loginID", "Test");
Params.put ("Password", "111111″");
Params.put ("website", "Http://www.bt285.cn″");
Params.put ("Name", "novel ″");
Params.put ("Voucherservice", Voucherservice);
Map extracontext = new HashMap ();
Extracontext.put (Actioncontext.parameters,params); try {proxy = Actionproxyfactory.getfactory (). Createactionproxy ("/http://www.5a520.cn user", "testaction",
Extracontext);
Proxy.setexecuteresult (FALSE);
Assertequals (Proxy.execute (), "success");
catch (Exception e) {e.printstacktrace (); @Test public void Testlogin () {testaction testaction = (testaction) proxy. Getaction ();
Assertequals (Testaction.login (), "success"); }
}
Note: The agent action must be created to perform the Proxy.execute () method, otherwise the parameter cannot be added to the actioncontext context. Because the Invocation.invoke () core method is executed in Proxy.execute (), traversing all interceptors in the execution action, including the parameter interceptor