关闭

单元测试之Action的测试

发表于:2007-10-29 13:33

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

        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,一个测试的例子就好了。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号