美文网首页
单元测试之加载H2数据库

单元测试之加载H2数据库

作者: 衣锦昼行 | 来源:发表于2019-06-19 18:04 被阅读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即可

相关文章

网友评论

      本文标题:单元测试之加载H2数据库

      本文链接:https://www.haomeiwen.com/subject/gbqlqctx.html