【发布时间】:2019-10-19 21:30:48
【问题描述】:
我正在使用 Spring Boot 开发一个微服务应用程序。 我的应用程序将使用 Postgres DB 进行生产配置,并使用 Spring Boot 自动测试 H2 DB。 因此,我的 pom.xml 包含两个依赖项(H2 + Postgres)。我尝试将 H2 依赖项与 tes 范围相关联,并将 Postgres 与运行时相关联,如下所示:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
我可以看到在运行 mvn test 时 Spring Boot 默认选择了我的单元测试环境中不存在的 postgres 数据库。这就是为什么我更喜欢使用 H2 来运行单元测试的原因。
是否有适当的方法告诉 Spring Boot 使用 H2 进行测试,否则使用 Postgres?
我不知道使用不同的 application.properties 文件(一个在 src/main/resources 中,另一个在 src/test/resources 中)是否可以解决问题。
【问题讨论】:
-
你的范围向后。
-
哦,对了!我会反转并检查。谢谢!
-
@chrylis 反转运行时/范围没有解决问题。正如 g00glen00b 所解释的,运行时范围依赖项也将在测试范围内可用。 H2和Postgres之间还是会有冲突
标签: java spring postgresql spring-boot h2