junit

JUnit 5 根据激活的 Profile 进行测试

1、概览 我们经常需要为开发和部署过程中的不同阶段创建不同的配置。在 Spring Boot 应用中,我们可以为每个不同的阶段定义一个 Spring Profile 并为其创建专门的测试。 在本教程中,我们将介绍如何使用 JUnit 5 基于激活的 Spring Profile 来进行测试。 2、项目设置 首先,在我们的项目中添加 spring-boot-starter-web 依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 现在,让我们创建一个简单的 Spring Boot 应用: @SpringBootApplication public class ActiveProfileApplication { public static void main (String [] args){ SpringApplication.run(Application.class); } } 最后,创建 application.yaml 配置文件。 3、Spring Profile Spring Profile 提供了一种方法,通过对每个环境的特定配置进行分组,来定义和管理不同的环境。 通过激活特定的 Profile,我们可以在不同的配置之间轻松切换。 3.1、在 Properties 中激活 Profile 我们可以在 application.yaml 文件中指定要激活的 profile: spring: profiles: active: dev 现在 Spring 将检索激活的 profile 的所有属性,并将所有专用 Bean 加载到 application context 中。