概述
Dbunit是一个基于JUnit的数据库集成测试框架。DBUnit 的设计理念就是在测试之前,给对象数据库植入我们需要的准备数据,最后,在测试完毕后,回溯到测试前的状态;它使数据库在测试过程之间处于一种已知状态,如果一个测试用例对数据库造成了破坏性影响,它可以帮助避免造成后面的测试失败或者给出错误结果。
Spring Test DbUnit提供了Spring Test Framework与DBUnit之间的集成。使用Spring Test DbUnit提供了注解驱动的数据库集成测试方式。
为了保证测试可模拟测试场景、可重复执行,我们需要一个简单容易的数据准备工具;一个隔离的干净的数据库环境。一般都是每次执行前都清掉数据库然后Dbunit(如果测试没有自己独立的库往往影响其他同事工作),测试完回滚到测试前的状态。所以这篇wiki后面会介绍内存数据库用于dbunit,这样就就可以保证每次测试时都可以有个相当干净的环境,而且不会影响其他人的开发。
关于Dbunit的使用在网上可以搜索到很多文章,http://yangzb.iteye.com/blog/947292 写的很详细,所以这里不讨论DbUnit的使用,而是直接介绍Spring Test Dbunit的是使用。
Spring Test Dbunit配置和使用
1、项目中引入依赖
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.0</version>
</dependency>
2、注册Spring Test Dbunit监听器
定义一个测试的基类,其他的测试类集成这个基类,在积累上使用@TestExecutionListeners注解注册Spring Test Dbunit监听器,Spring Test DbUnit提供的注解就可以被Spring Test处理。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath*:"classpath:applicationContext.xml" })
@TestExecutionListeners({
DbUnitTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class})
@DbUnitConfiguration(databaseConnection = "h2DataSource")
public class SpringTransactionalTestCase {
@Test
public void testToString() throws Exception {
}
}
3、数据源配置
如果在Spring Test DbUnit中不配置其他的数据源默认使用Spring容器中id="dataSource"的数据源,Spring Test DbUnit支持配置多数据源。
- 如果需要指定数据源,配置如下:
@DbUnitConfiguration(databaseConnection="h2DataSource") //参见上面的SpringTransactionalTestCase类
h2DataSource数据源需要在注解
@ContextConfiguration中配置的applicationContext配置文件中声明,我这里是用个专门的applicationContext配置文件(src/test/resource/applicationContext-mybatis-test.xml,业务代码中数据源的配置文件是src/java/resource/applicationContext-mybatis.xml)声明数据源,并测试类的@ContextConfiguration中只加载applicationContext-mybatis-test.xml替换业务代码中的applicationContext-mybatis.xml。这样做的原因是为了集成测试环境使用独立的内存数据库。
applicationContext-mybatis-test.xml配置文件如下:
<!--h2数据源--> <bean id="h2DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.h2.Driver" /> <!-- where the db will be placed (created automatically) --> <property name="url" value="jdbc:h2:./db/test" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> <!--主连接配置--> <bean id="h2SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="h2DataSource"/> <!-- 配置扫描Domain的包路径 --> <property name="typeAliasesPackage" value="com.and1.test.domain"/> <!-- 配置扫描Mapper XML的位置 --> <property name="mapperLocations" value="classpath*:com/and1/test/mapper/**/*.xml"/> <!-- 配置mybatis配置文件的位置 --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 注册类型转换器 --> <property name="typeHandlers"> <list> <ref bean="dateIntTypeHandler"></ref> </list> </property> </bean> <!-- 配置扫描Mapper接口的包路径 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="h2SqlSessionFactory" /> <property name="basePackage" value="com.and1.test.dao.mapper"/> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="h2SqlSessionFactory"/> </bean>
- 如果需要配置多数据源(spring-test-db