路是爬出來的

          [導(dǎo)入] 單元測試之實踐四 Action的測試

                 Action的測試是比較辛苦的。因為它依賴與其他的環(huán)境(比如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. }  





          有經(jīng)驗的程序員見到上面的代碼應(yīng)該就知道怎么測試了。

          我們只需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 路是爬出來的 閱讀(136) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 太白县| 高唐县| 毕节市| 通榆县| 兴隆县| 华坪县| 沛县| 紫金县| 陇南市| 开平市| 苍南县| 鹤壁市| 怀化市| 云浮市| 南川市| 信宜市| 梁河县| 萨迦县| 腾冲县| 孝昌县| 万州区| 资中县| 宣恩县| 海安县| 六安市| 五莲县| 巴林左旗| 双鸭山市| 陇西县| 泸州市| 宜丰县| 曲阳县| 蒲江县| 宁河县| 内江市| 罗城| 宣城市| 建始县| 大竹县| 和硕县| 阿拉善盟|