路是爬出来的

[导入] 单元测试之实践四 Action的测试

       Action的测试是比较辛苦的。因为它依赖与其他的环境(比如tomcat)。

       在我的印象中,基于struts的测试是很麻烦的,因为对于execute方法,你必须mock两个对象进去。

      还好。基于Webwork的测试相对简单些。

      下面让我们来测试一个例子吧

      

java 代码


 


  1.       Account account;  

  2. IAccountService accountService;  

  3. public void setAccount(Account account) {  

  4.     this.account = account;  

  5. }  

  6.   

  7. public void setAccountService(IAccountService accountService) {  

  8.     this.accountService = accountService;  

  9. }  

  10.   

  11. public String regist() throws Exception {  

  12.     if(account == null) {  

  13.         account = new Account();  

  14.         return INPUT;  

  15.     }  

  16.       

  17.     if(!validForm(account))   

  18.         return INPUT;  

  19.       

  20.     try {  

  21.         accountService.regist(account);  

  22.     } catch (ObjectExistsException e) {  

  23.         e.printStackTrace();  

  24.         return INPUT;  

  25.     }  

  26.       

  27.     return SUCCESS;  

  28. }  

  29.   

  30. private boolean validForm(Account e) {  

  31.     if(e.getName() == null || e.getName().trim().equals(""))  

  32.         return false;  

  33.     if(e.getPassword() == null || e.getPassword().trim().equals(""))  

  34.         return false;  

  35.     return true;  

  36. }  





有经验的程序员见到上面的代码应该就知道怎么测试了。

我们只需setAccount,跟setAccountService即可,

而Account本身来讲就是是个po,所以可以自己new一个

AccountService则可以mock一个。真是太完美了,我太喜好mock,它总是给我惊喜

java 代码


 


  1. package org.wuhua.action;  

  2.   

  3. import junit.framework.TestCase;  

  4.   

  5. import org.easymock.MockControl;  

  6. import org.wuhua.exception.ObjectExistsException;  

  7. import org.wuhua.model.Account;  

  8. import org.wuhua.service.IAccountService;  

  9.   

  10. import sms.king.AccountManager;  

  11.   

  12. import com.opensymphony.xwork.Action;  

  13.   

  14. public class AccountActionTest extends TestCase {  

  15.     private MockControl control;  

  16.     IAccountService accountService;  

  17.     protected void setUp() throws Exception {  

  18.         control = MockControl.createControl(IAccountService.class);  

  19.         accountService = (IAccountService) control.getMock();  

  20.            

  21.     }  

  22.       

  23.     public void testRegistOk() throws Exception {  

  24.         Account employee = new Account("name");  

  25.         employee.setPassword("password");  

  26.           

  27.        

  28.            

  29.           

  30.         accountService.regist(employee);  

  31.         control.setVoidCallable(1);  

  32.           

  33.         control.replay();  

  34.           

  35.         AccountAction action = new AccountAction();  

  36.         action.setAccount(employee);  

  37.         action.setAccountService(accountService);  

  38.           

  39.         assertEquals(Action.SUCCESS, action.regist());  

  40.           

  41.         control.verify();  

  42.     }  

  43.       

  44.     public void testRegistNameExists() throws Exception {  

  45.         Account employee = new Account("name");  

  46.         employee.setPassword("password");  

  47.           

  48.    

  49.        

  50.           

  51.         accountService.regist(employee);  

  52.         control.setThrowable(new ObjectExistsException(""));  

  53.           

  54.         control.replay();  

  55.           

  56.         AccountAction action = new AccountAction();  

  57.         action.setAccount(employee);  

  58.         action.setAccountService(accountService);  

  59.           

  60.         assertEquals(Action.INPUT, action.regist());  

  61.           

  62.         control.verify();  

  63.     }  

  64. }  



 

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


文章来源: http://wuhua.javaeye.com/blog/38623

posted on 2006-12-30 08:39 路是爬出来的 阅读(128) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: