<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    路是爬出來的

    單元測試之實踐四 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,一個測試的例子就好了。

    posted on 2006-12-30 09:06 路是爬出來的 閱讀(154) 評論(0)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: 6080午夜一级毛片免费看| 一本久久免费视频| 1000部无遮挡拍拍拍免费视频观看| 国产亚洲精品久久久久秋霞| 免费又黄又爽又猛大片午夜| 亚洲精品岛国片在线观看| 午夜不卡AV免费| 亚洲精品午夜国产VA久久成人| 中文字幕a∨在线乱码免费看| 中文亚洲AV片不卡在线观看| 四虎国产精品免费永久在线| 亚洲午夜在线电影| 日本三级2019在线观看免费| 亚洲欧美日韩一区二区三区| 四虎AV永久在线精品免费观看| 色吊丝性永久免费看码| 中文字幕在线亚洲精品 | 亚洲喷奶水中文字幕电影| 国产99视频精品免费观看7| 亚洲AV无码专区亚洲AV桃| 亚洲裸男gv网站| 一级毛片aaaaaa免费看| 国产成人亚洲综合一区| 亚洲阿v天堂在线2017免费| a毛片在线免费观看| 亚洲不卡视频在线观看| 国产免费一区二区三区VR| 你是我的城池营垒免费看 | 91香蕉国产线在线观看免费| 亚洲H在线播放在线观看H| 亚洲AⅤ优女AV综合久久久| 欧洲人成在线免费| 亚洲午夜无码久久久久小说| 亚洲综合亚洲综合网成人| 222www免费视频| 日韩在线一区二区三区免费视频| 亚洲AV无码一区二区二三区软件| 最近中文字幕mv免费高清电影 | 亚洲日韩精品无码专区| 久久久久亚洲?V成人无码| 国色精品卡一卡2卡3卡4卡免费|