h2单元测试

h2来模拟数据库

  1. pom.xml中加入依赖
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.190</version>
            <scope>test</scope>
        </dependency>
  1. main/resources下加入 application-test.yml,sql文件夹
  • application-test.yml代码如下:
spring:
  datasource:
    url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;
    platform: h2
    username: sa
    password:
    driverClassName: org.h2.Driver

    data: classpath:sql/data.sql
    schema: classpath:sql/schema.sql

h2:
  console:
    enabled: true
    path: /console
    settings:
      trace: false
      web-allow-others: false

mybatis:
  mapper-locations: classpath:mapper/*.xml
  config-location:  classpath:config/mybatis-config.xml

  • sql文件夹包含了数据库表定义文件和插入数据脚本
    注意:h2数据库语法和mysql差别较大,如果报错,请查看数据库脚本文件
  1. 编写单元测试时,需在类文件上加入注解 @ActiveProfiles("test")来选择测试yaml
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。