一般进行数据库测试我最喜欢的工具是dbunit + hsqldb
进行数据库测试的时候我觉得最关键的是你要能够控制数据库的
状态以确保在任何时候运行该测试都是正确的,
其中dbunit可以方便的增加和清除测试数据,
hsqldb是一个内存数据库,明显,让自己的测试脱离环境是一个好的方式至于你说的问题,我觉得跟测试无关,你应该好好的去看一下hibernate方面
的书,我推荐hibernate的参考手册
进行数据库测试的时候我觉得最关键的是你要能够控制数据库的
状态以确保在任何时候运行该测试都是正确的,
其中dbunit可以方便的增加和清除测试数据,
hsqldb是一个内存数据库,明显,让自己的测试脱离环境是一个好的方式至于你说的问题,我觉得跟测试无关,你应该好好的去看一下hibernate方面
的书,我推荐hibernate的参考手册
import com.untworld.esystem.model.*;import junit.framework.Assert;
import junit.framework.TestCase;
/**
* 测试EsServiceType实体Bean类的增、删、改、查。
*
* @author teatop (Email: [email protected])
*/
public class EsServiceTypeTest extends TestCase {
HsqlDbDao dao;
//初始化
protected void setUp() throws Exception {
super.setUp();
HsqlDbDao.initalDao(null);
dao = HsqlDbDao.getDao();
}
//junit结束时调用
protected void tearDown() throws Exception {
super.tearDown();
dao.closeSession();
}
/**
* 建立一个新的角色对象使用Dao在数据库中建立起该对象的数据,
* 并从原来的对象中取得分配的对象序号.
*/
public void testInsertEsServiceType() {
EsServiceType ec=new EsServiceType();
//no null
ec.setName("测试需求类型");
ec.setIsActive(true);
//nullable
ec.setCode("test code");
ec.setDescribe("test describe");
dao.save(ec);
boolean result = (ec.getServiceTypeId()!=null &&
ec.getServiceTypeId().intValue() >0 ); //判读是否建立对象成功
Assert.assertTrue(result);
}
/**
* 测试EsServiceType的实体Bean的查询功能,
* 并取出对象的中部分数据进行验证
*/
public void testQueryEsServiceType(){
String hql="from EsServiceType as ec where ec.serviceTypeId=" +
"(select max(ec1.serviceTypeId) from EsServiceType ec1)";
List list=dao.findByHql(hql);
if(list != null)
{
EsServiceType ec = (EsServiceType)list.get(0);
int tempId=ec.getServiceTypeId();
boolean result = ec.getServiceTypeId().equals(tempId);
//断言已经取得了数据
Assert.assertTrue(result);
}
}
/**
* 测试EsServiceType的实体Bean的更新功能,
* 并取出对象的中部分数据进行验证
*/
public void testUpdateEsServiceType(){
String hql="from EsServiceType as ec where ec.serviceTypeId=" +
"(select max(ec1.serviceTypeId) from EsServiceType ec1)";
List list=dao.findByHql(hql);
if(list != null)
{
EsServiceType ec = (EsServiceType)list.get(0);
ec.setName("修改单元");
dao.save(ec);
boolean result = ec.getName().equals("修改单元");
//断言已经修改成功
Assert.assertTrue(result);
}
}
/**
* 测试EsServiceType的实体Bean的删除功能,
* 并取出对象的中部分数据进行验证
*/
public void testDeleteEsServiceType(){
String hql="from EsServiceType as ec where ec.serviceTypeId=" +
"(select max(ec1.serviceTypeId) from EsServiceType ec1)";
List list=dao.findByHql(hql);
if(list != null)
{
EsServiceType ec = (EsServiceType)list.get(0);
dao.delete(ec);
//断言已经删除数据
Assert.assertFalse(false);
}
}
}这里有个例子
慢慢看吧