小编典典

在Spring中配置特定的内存数据库以进行测试

spring-boot

如何配置Spring Boot应用程序,以便在运行单元测试时它将使用内存数据库(例如H2 / HSQL),但是在运行Spring
Boot应用程序时,它将使用生产数据库[Postgre / MySQL]?


阅读 362

收藏
2020-05-30

共1个答案

小编典典

弹簧轮廓可用于此目的。这将是一种特定的方式:

具有特定于环境的属性文件:

application.properties

spring.profiles.active: dev

application-dev.properties

spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: update

spring.datasource.url: jdbc:mysql://localhost:3306/dbname
spring.datasource.username: username
spring.datasource.password: password

application-test.properties

spring.jpa.database: HSQL

在中同时具有 MySQLH2 驱动程序pom.xml,如下所示:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <scope>test</scope>
</dependency>

最后但并非最不重要的一点是,用注释Test类@ActiveProfiles("test")

2020-05-30