在 Spring 引导中在相同的 pom.xml 中管理 H2 和 Postgres
Manage H2 and Postgres in same pom.xml in Spring Boot
我正在使用 Spring Boot 开发微服务应用程序。
我的应用程序将使用 Postgres 数据库进行生产配置,并使用 Spring 启动自动测试 H2 数据库。
因此,我的 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 启动使用 H2 进行测试,否则使用 Postgres?
我不知道使用不同的 application.properties 文件(一个在 src/main/resources 中,另一个在 src/test/resources 中)是否可以解决问题。
您应该知道有多个 class 路径,例如:
- compile-timeclass路径,
- 运行时class路径,
- 测试class路径。
当您使用 <scope>runtime</scope>
时,依赖项将在 运行时 class 路径 和 测试 [=54] 中可用=]路径,如the documentation所述:
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
这意味着即使您正在执行测试,如果您使用 <scope>runtime</scope>
.
,Postgres 仍然会在您的 class 路径上
您提到的解决方案,通过提供两个单独的application.properties
是正确的选择。
在 src/main/resources/application.properties
内,您可以像这样配置数据源:
spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase
在 src/test/resources/application.properties
中,您可以像这样配置数据源:
spring.datasource.url=jdbc:h2:mydatabase
如果您需要更多 fine-grained 控制,可以使用 Spring 配置文件。例如,您可以使用名为 "testdb" 的配置文件,然后使用 @ActiveProfiles("testdb")
.
注释您的测试
现在您可以创建一个名为 application-testdb.properties
的文件并添加设置测试数据库所需的属性。
我正在使用 Spring Boot 开发微服务应用程序。 我的应用程序将使用 Postgres 数据库进行生产配置,并使用 Spring 启动自动测试 H2 数据库。 因此,我的 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 启动使用 H2 进行测试,否则使用 Postgres?
我不知道使用不同的 application.properties 文件(一个在 src/main/resources 中,另一个在 src/test/resources 中)是否可以解决问题。
您应该知道有多个 class 路径,例如:
- compile-timeclass路径,
- 运行时class路径,
- 测试class路径。
当您使用 <scope>runtime</scope>
时,依赖项将在 运行时 class 路径 和 测试 [=54] 中可用=]路径,如the documentation所述:
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
这意味着即使您正在执行测试,如果您使用 <scope>runtime</scope>
.
您提到的解决方案,通过提供两个单独的application.properties
是正确的选择。
在 src/main/resources/application.properties
内,您可以像这样配置数据源:
spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase
在 src/test/resources/application.properties
中,您可以像这样配置数据源:
spring.datasource.url=jdbc:h2:mydatabase
如果您需要更多 fine-grained 控制,可以使用 Spring 配置文件。例如,您可以使用名为 "testdb" 的配置文件,然后使用 @ActiveProfiles("testdb")
.
现在您可以创建一个名为 application-testdb.properties
的文件并添加设置测试数据库所需的属性。