在编写单测的过程中,我们应该尽可能的减少单测实例对第三方组件/服务的依赖。本章主要讲解springboot如何通过dbunit+h2实现脱离数据库独立执行单测实例。
1、pom.xml添加依赖,springboot默认已配置相关版本,只需要直接引入依赖即可
<!-- 单测依赖模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>