路是爬出來(lái)的

          單元測(cè)試之實(shí)踐四 Action的測(cè)試

                 Action的測(cè)試是比較辛苦的。因?yàn)樗蕾嚺c其他的環(huán)境(比如tomcat)。

                 在我的印象中,基于struts的測(cè)試是很麻煩的,因?yàn)閷?duì)于execute方法,你必須mock兩個(gè)對(duì)象進(jìn)去。

                還好。基于Webwork的測(cè)試相對(duì)簡(jiǎn)單些。

                下面讓我們來(lái)測(cè)試一個(gè)例子吧

                

          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àn)的程序員見(jiàn)到上面的代碼應(yīng)該就知道怎么測(cè)試了。

          我們只需setAccount,跟setAccountService即可,

          而Account本身來(lái)講就是是個(gè)po,所以可以自己new一個(gè)

          AccountService則可以mock一個(gè)。真是太完美了,我太喜好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,一個(gè)測(cè)試的例子就好了。

          posted on 2006-12-30 09:06 路是爬出來(lái)的 閱讀(155) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 卓资县| 东乡族自治县| 平定县| 米脂县| 隆回县| 灌云县| 邻水| 玉溪市| 清苑县| 西林县| 左云县| 集贤县| 沅陵县| 沐川县| 彭山县| 剑河县| 凌云县| 临澧县| 板桥市| 西畴县| 布拖县| 无为县| 乌拉特后旗| 青浦区| 凤城市| 清涧县| 栖霞市| 临城县| 兴海县| 中方县| 平顺县| 沙洋县| 房产| 扶沟县| 托里县| 额济纳旗| 博罗县| 曲靖市| 纳雍县| 海林市| 鸡西市|