DbUnit实践:Spring Test Dbunit,H2数据库

本文介绍了如何使用Spring Test Dbunit进行数据库集成测试,详细讲解了配置和使用步骤,包括数据源设置、DbUnit操作类型以及H2数据库的依赖和配置。通过这种方式,可以在每次测试前后维护一个干净的数据库环境,确保测试的可重复性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

       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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值