首页 > 解决方案 > Spring Test @ActiveProfiles 替代方案?

问题描述

我想为带有外部变量的测试设置活动配置文件 - 例如,VM 选项。我有任何变化吗?

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

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

有什么想法吗?

标签: javaspringspring-testspring-profiles

解决方案


您可能可以使用 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 {

}

推荐阅读