美文网首页JAVA_Spring
SpringBoot1 开发之h2内存数据库(单元测试使用)

SpringBoot1 开发之h2内存数据库(单元测试使用)

作者: kason_zhang | 来源:发表于2018-11-03 12:23 被阅读0次

一般开发时,可以准备两套数据库, 真实的数据操作的数据库, 测试使用的内存数据库(比如h2)
SpringBoot中针对这个h2数据库需要加入:

<!--内存数据库,测试使用 -->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

配置文件:
application

spring.profiles.active=dev

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=validate

logging.level.org.hibernate.SQL=debug

# Session 会话存储类型
spring.session.store-type=hash_map

# 关闭http 基本验证
security.basic.enabled=false

spring.profiles.active=dev说明我们使用开发版本的配置文件
application-dev.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xunwu?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

h2 内存数据库测试使用application-test.properties

spring.datasource.driver-class-name=org.h2.Driver
# 内存模式
spring.datasource.url=jdbc:h2:mem:test

# 指定我们预先创建的h2数据库 的具体数据
spring.datasource.schema=classpath:db/schema.sql
spring.datasource.data=classpath:db/data.sql

完成上述配置后, 单元测试类需要新增:@ActiveProfiles("test"),代表激活使用application-test.properties

@RunWith(SpringRunner.class)
@SpringBootTest
@Configuration
@ActiveProfiles("test")
public class KasonProjectApplicationTests {



}

相关文章

  • SpringBoot1 开发之h2内存数据库(单元测试使用)

    一般开发时,可以准备两套数据库, 真实的数据操作的数据库, 测试使用的内存数据库(比如h2)SpringBoot中...

  • 在Spring Boot使用H2内存数据库

    在Spring Boot使用H2内存数据库 在之前的文章中我们有提到在Spring Boot中使用H2内存数据库方...

  • 使用h2内存数据库 实现测试用例的无关性

    几个参考的博客 junit单元测试使用H2内存数据库 - 温故而知新,可以为师矣。 - ITeye博客 如何在s...

  • SpringBoot 接入H2数据库开发

    H2数据库简单介绍 h2 是一个流行的非常轻量级的优秀开源数据库,支持内存和文件模式。在开发和测试阶段使用起来非常...

  • springboot与jpa的整合

    在连接数据的时候使用的是h2内存数据库进行数据的查询操作,下面介绍一下内存数据库h2在springboot中的使用...

  • H2

    隔离数据库环境 使用 H2 内存数据库提高数据库操作速度 在 spring-test 中使用 spring-tx ...

  • h2错误提示:Table not found

    问题 使用h2做内存数据库时,查询某表,程序提示table不存在。 引用h2版本 启动数据库 初始化数据库 查询数...

  • In-Memory Zookeeper

    内存数据库-h2 内存数据库对比https://www.oschina.net/question/12_60371...

  • 单元测试之加载H2数据库

    由于单元测试在jenkins上无法连接远程数据库,所以单元测试需要建立一个虚假的数据库,H2数据库成为首选,下面对...

  • SpringBoot基础系列-整合H2

    原创文章,转载请标注出处:《SpringBoot基础系列-整合H2》 一般我们在测试的时候习惯于使用内存内存数据库...

网友评论

    本文标题:SpringBoot1 开发之h2内存数据库(单元测试使用)

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