【问题标题】:Springboot ActiveProfile annotation is triggering test regardless of active profile being used无论使用什么活动配置文件,Spring Boot Active Profile 注释都会触发测试
【发布时间】:2021-10-21 15:41:47
【问题描述】:

我的 Springboot REST API 应用程序使用 JUnit 5 的 RESTAssured 集成测试。我希望能够隔离这些 RESTAssured 测试,以便它们仅针对内存中的 H2 数据库运行,为此,我使用如下配置文件:

  • application.profile(将活动配置文件设置为“开发”或“生产”)
  • application-dev.profile(使用内存 H2 数据库进行测试的开发配置文件)
  • application-prod.profile(使用真实数据库的生产配置文件)

我正在尝试隔离我的 RestAssured 测试,以便当我运行 maven > install 时,它们只会在我使用“dev”配置文件时运行。

如果我使用“生产”配置文件,我不想运行它们,因为我不希望 RestAssured 测试针对真实数据库触发。

我的 RestAssured 测试类用 @ActiveProfiles("dev") 注释为:

@ActiveProfiles("dev")
class MyControllerRestAssuredTest { ... }

我希望当我的 application.profile 设置活动配置文件时:

  • spring.profiles.active=dev,maven > 安装将运行 MyControlerRestAssuredTest
  • spring.profiles.active=prod, maven > install 将 不 运行 MyControlerRestAssuredTest

但是,无论我如何设置我的活动配置文件,maven > install 都会运行MyControlerRestAssuredTest 测试。

我错过了什么?

【问题讨论】:

  • @ActiveProfiles 设置处于活动状态的配置文件。当您使用 JUnit 5 时,您可能希望改用 @DisableIf(@IfProfileValue 仅支持 JUnit 4)。
  • 您好,谢谢。我用@DisabledIf(expression = "#{environment['spring.profiles.active'] == 'prod'}") 注释了我的测试,并在我的src/test/java/application-test.properties 中设置了spring.profiles.active=prod,但是当我发出maven > install 时测试仍然运行
  • 尝试在您的application.properties 中设置spring.profiles.active = dev。另请查看this maven 和 Spring 引导配置文件集成的答案。

标签: spring-boot rest-assured spring-profiles


【解决方案1】:

尝试设置:

spring.profiles.active = dev

在application-test.properties

【讨论】:

  • 谢谢,但这是我尝试过的另一件事,但无论我是否在application-test.properties 中将spring.profiles.active 设置为dev 或prod(我添加到src/test/resources 文件夹,maven > install仍将运行该测试
  • 我也尝试在我的测试中设置@IfProfileValue(name ="spring.profiles.active", value ="dev"),但这也不起作用
  • 检查你的 maven 测试配置文件,它覆盖了 application-test.properties 中给出的 spring 配置文件
  • 那在哪里?我在 pom.xml 中没有看到任何与测试相关的内容
猜你喜欢
  • 1970-01-01
  • 2019-01-05
  • 1970-01-01
  • 2017-05-28
  • 2012-12-10
  • 2020-12-23
  • 1970-01-01
  • 2018-10-25
  • 2019-02-20
相关资源
最近更新 更多