Writing action test web use Cases

Source: Internet
Author: User
Tags execution http request

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.