【问题标题】:Spring Test @ActiveProfiles alternative?Spring Test @ActiveProfiles 替代方案?
【发布时间】:2021-10-08 08:14:50
【问题描述】:

我想为带有外部变量的测试设置活动配置文件 - 例如,VM 选项。我有什么变化可以做到吗?

变体 1:使用变量设置 @ActiveProfiles,例如 @ActiveProfile("${testProfile:test}") - 不起作用或者我不知道如何操作。

变体 2:具有任何上下文设置,例如 WebContextInitializer(不适用于测试)。

有什么想法吗?

【问题讨论】:

  • 您不想将 application.properties 用于不同的配置文件?
  • 怎么办?你什么意思?

标签: java spring spring-test spring-profiles


【解决方案1】:

你或许可以使用 ActiveProfilesResolver

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/ActiveProfilesResolver.html

请看下面的例子:

public class MyActiveProfilesResolver  implements ActiveProfilesResolver{
  @Override
  public String[] resolve(Class<?> testClass) {
      String os = System.getProperty("os.name");
      String profile = (os.toLowerCase().startsWith("windows")) ? "windows" : "other";
      return new String[]{profile};
  }
}


@ActiveProfiles(resolver = MyActiveProfilesResolver.class)
public class MyServiceTests {

}

【讨论】:

  • 不,我想从外部在@Act​​iveProfile("test") 中设置“test”。
  • 更新了@ВячеславЧернышов 希望对你有用
  • 好方法,但是如何将 VM 选项发送到解析器?
  • 你为什么不简单地使用jvm参数-Dspring.profiles.active?如果你使用 maven,你也可以在你的 pom 中为测试阶段设置它。见baeldung.com/spring-profiles。如果你想手动运行测试,你可以配置你的 IDE 来添加参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-27
  • 2015-11-10
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
相关资源
最近更新 更多