Spring Test @ActiveProfiles alternative?

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?

Upvotes: 1

Views: 333

Answers (1)

jcompetence
jcompetence

Reputation: 8383

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 {

}

Upvotes: 2

Related Questions