springboot+dbunit+h2集成测试简述

最近项目想弄个单元测试,但是数据测试准备很麻烦,有时候还存在多人操作,数据被污染的风险。网上找了一些资料,发现dbunit+h2就可以很好的解决这个问题。

h2的其它特征不说了,本地测试主要因为可以把它当成内存数据库,启动快,而且可以关闭持久化功能,每一个用例执行完随即还原到初始状态。做到测试时数据唯一,不会互相影响,有一个干净的数据环境。

废话不多说,我们直接说怎么应用到自己项目(maven项目)。

1、添加外部引用

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.4.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.dbunit</groupId>
            <artifactId>dbunit</artifactId>
            <version>2.5.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.springtestdbunit</groupId>
            <artifactId>spring-test-dbunit</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

2、配置项

1、配置h2数据源,驱动和数据库链接配置都换成h2
    datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    username: testh2
    password: testh2
    type: com.alibaba.druid.pool.DruidDataSource
2、main/recourses目录下 新增SCHEMA.sql配置,表名字段名最好大写,小写可能会找不到表名,有空的同学可以试下,参考如下:
DROP TABLE IF EXISTS TEST;

CREATE TABLE TEST (
      ID                 VARCHAR2(32) not null primary key,
      CUST_NO            VARCHAR2(20) not null
);

3、创建test目录

注意test目录包名要和实际测试服务类一致,我们需要新建一个java包和resources包。


image.png

3、创建test类

1、在test/java/com.*包中创建test类,
2、在test/resources下面创建这个测试方法需要的数据源,xml形式,表名同SCHEMA.sql中内容。H2详细介绍可以见官网http://www.h2database.com/html/main.html
3、注解标签不做概述
##类名注解标签
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Bootstrap.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class})
@DbUnitConfiguration(dataSetLoader = ReplacementDataSetLoader.class,databaseConnection={"dataSource"})
public class IaCombineIncomdeTest {
}


##方法注解标签
  @Test
    @DatabaseSetup(connection = "dataSource",type = DatabaseOperation.CLEAN_INSERT, value = { "classpath:conf/insert_ia_combine_income.xml" })
    @ExpectedDatabase(override = false,connection = "dataSource",value = "classpath:conf/update_ia_combine_income.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
    @DatabaseTearDown(connection = "dataSource",type = DatabaseOperation.DELETE_ALL, value = { "classpath:conf/ia_combine_income.xml" })
    public void updateTest(){
        try {
            IaCombineIncome iaCombineIncome = new IaCombineIncome();
            iaCombineIncome.setId("6");
            iaCombineIncome.setCustName("测试修改1");
            iaCombineIncome.setCombineCode("22");

            IaCombineIncome iaResult = iaCombineIncomeService.update(iaCombineIncome);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

    }
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
    <TEST id="6" CUST_NO="19" />
</dataset>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容