路是爬出來的

          單元測試之實踐三 Service的測試

          測試Service,因為Service依賴的Dao, 所以只需Mock一個Dao即可。在這里我詳細的介紹關于注冊這個功能的測試

          java 代碼


           


          1. public interface IAccountService extends IBaseService {  

          2.       Account findAccountById(String id);  

          3.       Account findAccounByName(String name);  

          4.       void regist(Account account) throws ObjectExistsException;  

          5. }  



             注冊功能的實現。
          java 代碼


           


          1. public void regist(Account account) throws ObjectExistsException {  

          2.     if(accountDao.findAccounByName(account.getName()) != null)  

          3.         throw new ObjectExistsException("User's name is exists!");  

          4.       

          5.     accountDao.save(account);  

          6. }  





          測試代碼
          java 代碼


           


          1.     protected void setUp() throws Exception {  

          2.         control = MockControl.createControl(IAccountDao.class);  

          3.         accountDao = (IAccountDao) control.getMock();  

          4.         as = new AccountService();  

          5.         as.setAccountDao(accountDao);  

          6.     }  

          7.   

          8.   

          9. public void testFindAccountByName() {  

          10.         String name = "wuhua";  

          11.         accountDao.findAccounByName(name);  

          12.         Account a = new Account("wuhua");  

          13.         a.setId(name);  

          14.         control.setReturnValue(a);  

          15.         control.replay();  

          16.         Account at = as.findAccounByName(name);  

          17.         Assert.assertEquals(name, at.getId());  

          18.         Assert.assertEquals(a, at);  

          19.         control.verify();  

          20.     }  





          首先我們建立一個關鍵字查詢,name="wuhua";

          然后調用Dao的方法,

          然后自定義返回一個自己預期的對象,

          最后通過比較這個對象判斷結果是否是自己想要的

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


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


          網站導航:
           
          主站蜘蛛池模板: 徐水县| 西乡县| 咸阳市| 建湖县| 深圳市| 寿宁县| 神池县| 开江县| 盘山县| 绥德县| 志丹县| 鸡泽县| 庆阳市| 南开区| 贡山| 房产| 伽师县| 札达县| 灌阳县| 虞城县| 东兰县| 福鼎市| 神池县| 平凉市| 垫江县| 长治县| 略阳县| 凭祥市| 桐梓县| 清远市| 红原县| 临沂市| 台中市| 孟津县| 八宿县| 离岛区| 霍邱县| 禹州市| 剑河县| 南召县| 德兴市|