路是爬出來的

          單元測試之實踐三 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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 武平县| 安图县| 嘉善县| 阳新县| 奉化市| 松阳县| 哈密市| 泰和县| 平罗县| 博客| 喀喇沁旗| 盘锦市| 屯昌县| 山阳县| 房产| 佳木斯市| 博兴县| 宿松县| 津南区| 青龙| 大埔县| 杭州市| 报价| 淅川县| 潍坊市| 马边| 铜鼓县| 改则县| 蒙阴县| 库尔勒市| 太和县| 张掖市| 茌平县| 荣昌县| 乡宁县| 青浦区| 项城市| 惠州市| 华蓥市| 师宗县| 淮安市|