??????????????????????DAO?????
???????????? ???????[ 2013/5/15 14:00:47 ] ????????
????DAO?????????????????ɡ????DAO???????????DAO??????????????????????????????????SQL?????????????????????????????Mock?????????????????
??????????????????????????????????????????????????????????????????????????????????????????????????
?????????????????plain testcase??????????????????????????????????????????ò???????????????????????????bean??get/set?????ɡ?
??????????????????????????????dao????????????????????????dao????????????????????????????mock?????????? ?????????????DAO?????????????????????????????????sql????????sequence?????????????Щ???????????????????????? ???????????????????????????????????????????????????????
???????????dao???????????????ν?????????????????????????????????????????????????????????????????????????????????????????????
??????????綼???????DAO??????????????????á?
??????????????????????????????????????????????????????????
??????????????????????????????????????ɡ????????????????????????????????????λ?????????????ü??????????????а????????e??????????????????????????????
???????????????鵽????????????????
1. public interface IAccountDao extends IBaseDao {
2. public Account findAccountById(String id);
3. public Account findAccounByName(String name);
4. }
?????????
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;
}
}
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();
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11