单元测试之加载H2数据库
2019-06-19 本文已影响0人
衣锦昼行
由于单元测试在jenkins上无法连接远程数据库,所以单元测试需要建立一个虚假的数据库,H2数据库成为首选,下面对其的一些操作做一些记录
首先要在pom.xml中添加依赖
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>test</scope>
</dependency>
然后在test文件夹下面建一个application.properties,这样可以不影响主程序的运行
配置如下:
#************H2 Begin****************
#db schema
spring.datasource.schema=classpath:testdb/schema.sql
#db data
spring.datasource.data=classpath:testdb/data.sql
#remote visit
spring.h2.console.settings.web-allow-others=true
#console url
spring.h2.console.path=/h2-console
#default true
spring.h2.console.enabled=true
spring.h2.console.settings.trace=true
#db url,default :jdbc:h2:mem:testdbsa
spring.datasource.url=jdbc:h2:mem:apim;MODE=MySQL
#driver default:org.h2.Driver
spring.datasource.driver-class-name=org.h2.Driver
#default sa
spring.datasource.username=sa
#default null
spring.datasource.password=
#************H2 End****************
然后配置好两个数据库,即create &insert即可