---
title: " 单元测试之实践四 Action的测试"
author: "flash7783"
date: "2010-08-30T10:14:59Z"
tags: []
categories: ["研发管理"]
slug: "action"
draft: false
lastmod: "2010-08-30T10:14:59Z"
id: 750485
url: "https://flash7783.iteye.com/blog/750485"
---

Action的测试是比较辛苦的。因为它依赖与其他的环境（比如tomcat）。  
       在我的印象中，基于struts的测试是很麻烦的，因为对于execute方法，你必须mock两个对象进去。  
      还好。基于Webwork的测试相对简单些。  
      下面让我们来测试一个例子吧  
      

java 代码

 

01. Account account;
02. IAccountService accountService;
03. **public** **void** setAccount(Account account) {
04. **this**.account = account;
05. }
06. **public** **void** setAccountService(IAccountService accountService) {
07. **this**.accountService = accountService;
08. }
09. **public** String regist() **throws** Exception {
10. **if**(account == **null**) {
11. account = **new** Account();
12. **return** INPUT;
13. }
14. **if**(!validForm(account))
15. **return** INPUT;
16. **try** {
17. accountService.regist(account);
18. } **catch** (ObjectExistsException e) {
19. e.printStackTrace();
20. **return** INPUT;
21. }
22. **return** SUCCESS;
23. }
24. **private** **boolean** validForm(Account e) {
25. **if**(e.getName() == **null** || e.getName().trim().equals(""))
26. **return** **false**;
27. **if**(e.getPassword() == **null** || e.getPassword().trim().equals(""))
28. **return** **false**;
29. **return** **true**;
30. }

有经验的程序员见到上面的代码应该就知道怎么测试了。  
我们只需setAccount，跟setAccountService即可，  
而Account本身来讲就是是个po，所以可以自己new一个  
AccountService则可以mock一个。真是太完美了，我太喜好mock，它总是给我惊喜

java 代码

 

01. **package** org.wuhua.action;
02. **import** junit.framework.TestCase;
03. **import** org.easymock.MockControl;
04. **import** org.wuhua.exception.ObjectExistsException;
05. **import** org.wuhua.model.Account;
06. **import** org.wuhua.service.IAccountService;
07. **import** sms.king.AccountManager;
08. **import** com.opensymphony.xwork.Action;
09. **public** **class** AccountActionTest **extends** TestCase {
10. **private** MockControl control;
11. IAccountService accountService;
12. **protected** **void** setUp() **throws** Exception {
13. control = MockControl.createControl(IAccountService.**class**);
14. accountService = (IAccountService) control.getMock();
15. }
16. **public** **void** testRegistOk() **throws** Exception {
17. Account employee = **new** Account("name");
18. employee.setPassword("password");
19. accountService.regist(employee);
20. control.setVoidCallable(1);
21. control.replay();
22. AccountAction action = **new** AccountAction();
23. action.setAccount(employee);
24. action.setAccountService(accountService);
25. assertEquals(Action.SUCCESS, action.regist());
26. control.verify();
27. }
28. **public** **void** testRegistNameExists() **throws** Exception {
29. Account employee = **new** Account("name");
30. employee.setPassword("password");
31. accountService.regist(employee);
32. control.setThrowable(**new** ObjectExistsException(""));
33. control.replay();
34. AccountAction action = **new** AccountAction();
35. action.setAccount(employee);
36. action.setAccountService(accountService);
37. assertEquals(Action.INPUT, action.regist());
38. control.verify();
39. }
40. }

   
ok，一个测试的例子就好了。