Home > front end >  Spring Test @ActiveProfiles alternative?
Spring Test @ActiveProfiles alternative?

Time:10-08

I want to set active profiles for tests with outside variables - for example, VM options. Have I any variations to do it?

Variant 1: set @ActiveProfiles with variable, like @ActiveProfile("${testProfile:test}") - not working or I don't know how.

Variant 2: with any context settings, something like WebContextInitializer (not working for tests).

Have any ideas?

CodePudding user response:

You can probably use ActiveProfilesResolver

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

See example below:

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 {

}
  • Related