使用DBUnit测试SpringDao

使用DBUnit测试SpringDao

环境是这样的:

一个DAO接口 ,一个DAO的缺省实现类,这个实现了DAO接口的所有方法,并使用了泛型,所以其他的DAO实现类都省下了,不用写了.因为所有的操作都在这个缺省实现类里面完成了.只需要在Spring 配置文件中配置一下就可以了。因为使用Hibernate,所以表之间的关系还是要测试的。测试也就离不开Spring环境了。还好Spring提供了好多测试类可以扩展,我使用的是AbstractTransactionalDataSourceSpringContextTests,据说它可以保持数据的清洁,操作完之后就回滚。跟据我的测试在没有加flush()时候它好象根本就没有插入到数据库中,在加入flush()之后数据还是保留在数据库中,没有回滚(删除)掉。测试的结果:

没加入flush() :

[2007-09-06 15:01:31] cn.xiangyunsoft.sysadmin.dao.impl.UserDaoTest : [INFO ] - Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@11f91ac]; default rollback = true
[2007-09-06 15:01:31] cn.xiangyunsoft.sysadmin.dao.impl.UserDaoTest : [INFO ] - Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@11f91ac]; default rollback = true
[2007-09-06 15:01:31] cn.xiangyunsoft.business.dao.impl.DefaultAbstractDaoImpl : [DEBUG] - save 测试部门[01] success :01
[2007-09-06 15:01:31] cn.xiangyunsoft.business.dao.impl.DefaultAbstractDaoImpl : [DEBUG] - save 测试部门[01] success :01
[2007-09-06 15:01:31] cn.xiangyunsoft.business.dao.impl.DefaultAbstractDaoImpl : [DEBUG] - save 测试[ 001 ] success :001
[2007-09-06 15:01:31] cn.xiangyunsoft.business.dao.impl.DefaultAbstractDaoImpl : [DEBUG] - save 测试[ 001 ] success :001
保存成功 :001
[2007-09-06 15:01:31] cn.xiangyunsoft.sysadmin.dao.impl.UserDaoTest : [INFO ] - Rolled back transaction after test execution
[2007-09-06 15:01:31] cn.xiangyunsoft.sysadmin.dao.impl.UserDaoTest : [INFO ] - Rolled back transaction after test execution

按照这个提示应该是成功了。可是缺了插入和删除的语句(因为我的Hibernate是showSql的),感觉并没有真的向数据库里写入数据。感觉不放心!!!

加入flush() :
2007-09-06 15:03:44] cn.xiangyunsoft.sysadmin.dao.impl.UserDaoTest : [INFO ] - Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@11f91ac]; default rollback = true
[2007-09-06 15:03:44] cn.xiangyunsoft.sysadmin.dao.impl.UserDaoTest : [INFO ] - Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@11f91ac]; default rollback = true
Hibernate: insert into Department (Manager, Name, Parent, Tel, Id) values (?, ?, ?, ?, ?)
[2007-09-06 15:03:44] cn.xiangyunsoft.business.dao.impl.DefaultAbstractDaoImpl : [DEBUG] - save 测试部门[01] success :01
[2007-09-06 15:03:44] cn.xiangyunsoft.business.dao.impl.DefaultAbstractDaoImpl : [DEBUG] - save 测试部门[01] success :01
Hibernate: insert into Users (Departmentid, Name, Password, Id) values (?, ?, ?, ?)
[2007-09-06 15:03:44] cn.xiangyunsoft.business.dao.impl.DefaultAbstractDaoImpl : [DEBUG] - save 测试[ 001 ] success :001
[2007-09-06 15:03:44] cn.xiangyunsoft.business.dao.impl.DefaultAbstractDaoImpl : [DEBUG] - save 测试[ 001 ] success :001
保存成功 :001
[2007-09-06 15:03:44] cn.xiangyunsoft.sysadmin.dao.impl.UserDaoTest : [INFO ] - Rolled back transaction after test execution
[2007-09-06 15:03:44] cn.xiangyunsoft.sysadmin.dao.impl.UserDaoTest : [INFO ] - Rolled back transaction after test execution

这回出现在插入数据的语句了。但是操作完的数据还在数据库里呆着呢!!!

为了保险起见,还要自己来清理垃圾吧!所以引入DBUnit

在原有的测试之前通过DBUnit准备数据,在测试之后清量数据。真是任劳任怨!

看一下代码:这个是Dao的测试基本类,其他的测试都继续这个就可以了。

package test.support;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

/**
* Dao 测试基础类
*
* @author libo <br>
* @version 1.0 2007-9-6<br>
*/
public abstract class BaseDaoTest extends
AbstractTransactionalDataSourceSpringContextTests {

private ApplicationContext ctx;

private BasicDataSource dataSource;

private IDatabaseConnection connection;

public void execute(String path, DatabaseOperation oper) {
IDataSet dataset;
try {
dataset = new FlatXmlDataSet(new FileInputStream(path));
if (connection == null) {
if (dataSource == null)
dataSource = (BasicDataSource) getBean("dataSource");
if (dataSource != null) {
connection = new DatabaseConnection(this.dataSource
.getConnection());

// connection = new DatabaseConnection(this.dataSource
// .getConnection(), "schema");

} else
System.out.println("date source is null !!!!");
}
if (connection != null) {
// System.out.println("开始执行");
oper.execute(connection, dataset);

} else
System.out.println("connection is null!!!");

} catch (DataSetException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DatabaseUnitException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

protected String[] getConfigLocations() {
return new String[] { "classpath:/applicationContext_test.xml",
"classpath:/beans.xml" };
}

public Object getBean(String beanName) {
if (ctx == null)
ctx = new FileSystemXmlApplicationContext(getConfigLocations());
return ctx.getBean(beanName);
}

}

来看一个具体的测试类:

public class UserDaoTest extends BaseDaoTest {
private String dataSet = "bin/cn/xiangyunsoft/sysadmin/dao/impl/user.xml";//DBUnit需用的文件的位置

private IDao<User> dao = (IDao<User>) getBean("userDao");
public void testSaveOrUpdate() {
execute(dataSet, DatabaseOperation.DELETE_ALL); //测试前准备测试数据环境
Department department =new Department();
department.setId("01");
department.setName("测试部门");
dao.save(department);

User dep = new User();
dep.setId("001");
dep.setName("测试");
dep.setDepartment(department);
Serializable s = dao.save(dep);
System.out.println("保存成功 :" + s);
assertNotNull(s);
execute(dataSet, DatabaseOperation.DELETE); //清除测试时的数据
}

现在来看测试的效果还是可以的。不知道以后遇到更复杂的表好不好用。


Technorati : dao, dbunit

你可能感兴趣的:(使用DBUnit测试SpringDao)