前陣子發(fā)表過 我的第一個真正意義上的測試 。
里面對于測試Service大家是沒有意義的,對于測試DAO層則表現(xiàn)各有各的看法。
比如
測試DAO不如連數(shù)據(jù)庫一起測試吧。因為DAO測試的目的不是DAO接口實現(xiàn)對不對,而是測試是否如你預期的發(fā)送了SQL,如你預期的返回了結(jié)果集。這個時候你Mock之后,測試就沒有意義了。
wuhua 寫道
分層的原因很多。這里我的看法片面就不說了
但對于mock來說是有莫大好處的。
比如service測試的時候完全可以做到隔離數(shù)據(jù)庫,
我現(xiàn)在的意思是,
居然Service可以隔離Dao層,也就是說Dao層也是可以做到隔離相關(guān)的數(shù)據(jù)實現(xiàn)的。也是可以mock一個對象。而并非用實際的連接去代 替。如果我們的邏輯沒出錯的話,測試就算通過了,至于數(shù)據(jù)層的檢測,那就不關(guān)我們的事情了,比如Hibernate由Hibernate去test, Spring由Spring去Test,Oracle由它自己去做。干自己的事情,別趟其他渾水。這樣不是瀟灑很多嗎
但是數(shù)據(jù)庫的測試畢竟比較特殊,記住測試的目的是確保你的代碼質(zhì)量,如果你確定你的這樣測就沒問題了,那無話可說,否則就盡量多的測試。
事實上,最原始的單元測試(plain testcase)就是用來測方法,測業(yè)務(wù)邏輯的,如果有邏輯就測,沒邏輯就不用測了,同樣的道理,相信你不會去測一個bean的get/set方法吧。
記住你測試的目的和動機,如果你認為測試dao層是為了測你的邏輯(你確定你的dao的實現(xiàn)代碼是否真的存在邏輯),那你就mock吧,但是,我 們更相信,我們測DAO層,更應該是測訪問數(shù)據(jù)庫的情況,你如連接,sql是否正確,sequence是否正確等,而這些你必須要真正的連接數(shù)據(jù)庫,也因 此,我們一般都是直接訪問數(shù)據(jù)庫來測試的,當然,如果可能你可以采用內(nèi)存庫。
事實上,我們對dao的測試,一般都進行所謂的的集成單元測試。我認為,你應該確定好你的測試策略,然后在去采用相應的測試方法。我在目前的開發(fā)中就是采用這樣的方式測的。
上面兩個大哥都建議測試DAO的時候還是連接數(shù)據(jù)庫為好。
但個人認為上面兩個大哥的單元測試以非純正的單元測試了,而是集成單元測試。
其實說白了,測試這東西只是為了項目更好,更快的完成。至于是否要求純單元,或者是集成單元測試,則看各位的需要,如果覺得集成單元測試對項目有幫助,那就用吧,現(xiàn)在發(fā)現(xiàn)對這個已經(jīng)沒有明顯的界限了。
不理會它了,現(xiàn)在回歸到我們用戶注冊的例子。
java 代碼
- 1. public interface IAccountDao extends IBaseDao {
- 2. public Account findAccountById(String id);
- 3. public Account findAccounByName(String name);
- 4. }
實際實現(xiàn)代碼
java 代碼
- package org.wuhua.dao.impl;
-
- import java.util.List;
-
- import org.wuhua.dao.IAccountDao;
- import org.wuhua.model.Account;
-
- public class AccountDao extends BaseDao implements IAccountDao {
- public Account findAccountById(String id) {
- return (Account) this.getHibernateTemplate().get(Account.class, id) ;
- }
-
- public Account findAccounByName(String name) {
- List l = this.getHibernateTemplate().find("from Account as a where a.name=?", name);
- if(l != null && l.size() >=1)
- return (Account) l.get(0);
- else
- return null;
- }
- }
java 代碼
- package org.wuhua.dao;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import junit.framework.Assert;
- import junit.framework.TestCase;
-
- import org.easymock.MockControl;
- import org.easymock.classextension.MockClassControl;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- import org.wuhua.dao.impl.AccountDao;
- import org.wuhua.model.Account;
-
-
-
- public class AccountDaoTest extends TestCase {
-
- private AccountDao accountDao;
- private org.springframework.orm.hibernate3.HibernateTemplate ht;
- private MockControl control;
-
- protected void setUp() throws Exception {
- control = MockClassControl.createControl(HibernateTemplate.class);
- ht = (HibernateTemplate) control.getMock();
- accountDao = new AccountDao();
- accountDao.setHibernateTemplate(ht);
- }
-
- protected void tearDown() throws Exception {
-
- }
-
- public void testFindAccountById(){
- Account a = new Account("wuhua");
- a.setId("10");
-
- ht.get(Account.class, a.getId());
-
- control.setReturnValue(a);
-
- control.replay();
-
- Account result = accountDao.findAccountById(a.getId());
-
- assertNotNull(result);
-
- Assert.assertEquals(a.getId(),result.getId());
- Assert.assertEquals(a, result);
-
- control.verify();
-
- }
-
- public void testFindAccountByName(){
- Account a = new Account("wuhua");
-
- ht.find("from Account as a where a.name=?", a.getName());
- List l = new ArrayList();
- l.add(a);
- control.setReturnValue(l);
-
- control.replay();
-
- Account result = accountDao.findAccounByName(a.getName());
-
- Assert.assertEquals(a.getId(),result.getId());
- Assert.assertEquals(a, result);
-
- control.verify();
-
- }
- }
|