0

I am looking for a solution to automatically add the environment variable SPRING_PROFILES_ACTIVE="test" when running unit-tests. The solution should fulfill the following criteria :

  1. Ideally it should be configured via maven pom.xml
  2. If 1 is not possible configuration should be done for IntelliJ via configuration file in the project not via UI setting
  3. The particular environment variable should only be set when running unit tests not when generally launching the app.

Any idea on how to approach this goal is appreciated.

Best Andy

Andy
  • 9,483
  • 12
  • 38
  • 39
  • Doesn't an application properties file in `src/test/resources` fulfill your needs? – Seelenvirtuose Oct 26 '21 at 15:48
  • https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config and this https://stackoverflow.com/questions/11874017/controlling-a-project-with-maven-and-spring-how-to-set-spring-config-file-using can help you – Harry Coder Oct 26 '21 at 16:57

1 Answers1

0

The SPRING_PROFILES_ACTIVE is a property value that should be set in a file like application-test.properties or application-test.yml

In a yml file it would look like,

spring:
  profiles:
    active: test

Additionally, there are specific annotations to help identify certain classes/methods as test specific such as @Profile("test") or @ActiveProfiles("test").

duppydodah
  • 165
  • 1
  • 3
  • 17
  • Thanks, for your answer. Your recommendation for using the application-test.yml file requires every UnitTest-class to be annotated with @ActiveProfiles("test"), right? Is there a way to use the profile "test" by default for tests? – Andy Oct 26 '21 at 17:01
  • No, using an application-test yml certainly does not require annotating each class with @ActiveProfiles but, you may need some other type of basic annotation for spring to identify such as @RunWith(SpringRunner.class) – duppydodah Oct 26 '21 at 17:15