Spring-boot default profile for integration tests

JavaSpringSpring Boot

Java Problem Overview


Spring-boot utilizes Spring profiles which allow for instance to have separate config for different environments. One way I use this feature is to configure test database to be used by integration tests. I wonder however is it necessary to create my own profile 'test' and explicitly activate this profile in each test file? Right now I do it in the following way:

  1. Create application-test.properties inside src/main/resources

  2. Write test specific config there (just the database name for now)

  3. In every test file include:

    @ActiveProfiles("test")
    

Is there a smarter / more concise way? For instance a default test profile?

Edit 1: This question pertains to Spring-Boot 1.4.1

Java Solutions


Solution 1 - Java

As far as I know there is nothing directly addressing your request - but I can suggest a proposal that could help:

You could use your own test annotation that is a meta annotation comprising @SpringBootTest and @ActiveProfiles("test"). So you still need the dedicated profile but avoid scattering the profile definition across all your test.

This annotation will default to the profile test and you can override the profile using the meta annotation.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest
@ActiveProfiles
public @interface MyApplicationTest {
  @AliasFor(annotation = ActiveProfiles.class, attribute = "profiles") String[] activeProfiles() default {"test"};
}

Solution 2 - Java

Another way to do this is to define a base (abstract) test class that your actual test classes will extend :

@RunWith(SpringRunner.class)
@SpringBootTest()
@ActiveProfiles("staging")
public abstract class BaseIntegrationTest {
}

Concrete test :

public class SampleSearchServiceTest extends BaseIntegrationTest{

    @Inject
    private SampleSearchService service;

    @Test
    public void shouldInjectService(){
        assertThat(this.service).isNotNull();
    }
} 

This allows you to extract more than just the @ActiveProfiles annotation. You could also imagine more specialised base classes for different kinds of integration tests, e.g. data access layer vs service layer, or for functional specialties (common @Before or @After methods etc).

Solution 3 - Java

You could put an application.properties file in your test/resources folder. There you set

spring.profiles.active=test

This is kind of a default test profile while running tests.

Solution 4 - Java

There are two approaches.

Load from config/

(2022 update, tested against Spring Boot 2.6)

Along with the approach below, you can also add config to src/test/resources/config/application.yml

src/
├── main/
│   ├── java/
│   │   └── ...
│   └── resources/
│       └── application.yml <- default properties, always loaded
└── test/
    ├── java/
    │   └── ...
    └── resources/
        └── config/
            └── application.yml <- test properties, will override the defaults

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files

> Spring Boot will automatically find and load application.properties and application.yaml files from the following locations when your application starts: > > 1. From the classpath > 1. The classpath root > 2. The classpath /config package > 2. From the current directory > 1. The current directory > 2. The /config subdirectory in the current directory > 3. Immediate child directories of the /config subdirectory > > The list is ordered by precedence (with values from lower items overriding earlier ones). Documents from the loaded files are added as PropertySources to the Spring Environment.

Manual import using spring.config.import

(original answer from 2021, tested against Spring Boot 2.4)

One solution is to have 3 properties files and to import

  • src/main/resources/application.yml - contains the application's default props
  • src/test/resources/application.yml - sets the profile to 'test', and imports properties from 'main'
  • src/test/resources/application-test.yml - contains test-specific profiles, which will override 'main'

Here is the content of src/test/resources/application.yml:

# for testing, set default profile to 'test'
spring.profiles.active: "test"
# and import the 'main' properties
spring.config.import: file:src/main/resources/application.yml

For example, if src/main/resources/application.yml has the content

ip-address: "10.7.0.1"
username: admin

and src/test/resources/application-test.yml has

ip-address: "999.999.999.999"
run-integration-test: true

Then (assuming there are no other profiles)...

when running tests,

profiles=test
--
ip-address=999.999.999.999
username=admin
run-integration-test=true

and when running the application normally

profiles=none
--
ip-address=10.7.0.1
username=admin
run-integration-test <undefined>

Note: if src/main/resources/application.yml contains spring.profiles.active: "dev", then this won't be overwritten by src/test/resources/application-test.yml

Solution 5 - Java

A delarative way to do that (In fact, a minor tweek to @Compito's original answer):

  1. Set spring.profiles.active=test in test/resources/application-default.properties.
  2. Add test/resources/application-test.properties for tests and override only the properties you need.

Solution 6 - Java

If you use maven, you can add this in pom.xml:

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-failsafe-plugin</artifactId>
			<configuration>
				<argLine>-Dspring.profiles.active=test</argLine>
			</configuration>
		</plugin>
        ...

Then, maven should run your integration tests (*IT.java) using this arugument, and also IntelliJ will start with this profile activated - so you can then specify all properties inside

application-test.yml

and you should not need "-default" properties.

Solution 7 - Java

You can put your test specific properties into src/test/resources/config/application.properties.

The properties defined in this file will override those defined in src/main/resources/application.properties during testing.

For more information on why this works have a look at Spring Boots docs.

Solution 8 - Java

To activate "test" profile write in your build.gradle:

    test.doFirst {
        systemProperty 'spring.profiles.active', 'test'
        activeProfiles = 'test'
    }

Solution 9 - Java

In my case I have different application.properties depending on the environment, something like:

application.properties (base file)
application-dev.properties
application-qa.properties
application-prod.properties

and application.properties contains a property spring.profiles.active to pick the proper file.

For my integration tests, I created a new application-test.properties file inside test/resources and with the @TestPropertySource({ "/application-test.properties" }) annotation this is the file who is in charge of picking the application.properties I want depending on my needs for those tests

Solution 10 - Java

Another programatically way to do that:

  import static org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME;

  @BeforeClass
  public static void setupTest() {
    System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "test");
  }

It works great.

Solution 11 - Java

If you simply want to set/use default profile at the time of making build through maven then, pass the argument -Dspring.profiles.active=test Just like

> mvn clean install -Dspring.profiles.active=dev

Solution 12 - Java

I've usually done a base class for all integration tests with common code and annotations. Do not forget make it abstract in order not to instatiate. E.g:

@SpringBootTest
@Transactional
@AutoConfigureMockMvc
@ActiveProfiles("test")
public abstract class AbstractControllerTest {

    @Autowired
    protected MockMvc mockMvc;

    protected ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
        return mockMvc.perform(builder);
    }
}

// All annotations are inherited
class AccountControllerTest extends AbstractControllerTest {
....

Solution 13 - Java

The best solution I have found is the last suggestion here: https://inspeerity.com/blog/setting-default-spring-profile-for-tests-with-override-option/ The author also desribes the problem very clearly and discusses the downside of every other approach I can think of.

Create a file application-default.properties in your test resources, containing a single line:

spring.profiles.active=test

This takes advantage of the fact that Spring automatically enables a "default" profile if no other profiles were explicitly set. Now, your application-test.properties file will be used by default, for all tests.

Solution 14 - Java

Add spring.profiles.active=tests in your application.properties file, you can add multiple properties file in your spring boot application like application-stage.properties, application-prod.properties, etc. And you can specify in your application.properties file while file to refer by adding spring.profiles.active=stage or spring.profiles.active=prod

you can also pass the profile at the time running the spring boot application by providing the command:

java -jar -Dspring.profiles.active=local build/libs/turtle-rnr-0.0.1-SNAPSHOT.jar

According to the profile name the properties file is picked up, in the above case passing profile local consider the application-local.properties file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPiotr ZakrzewskiView Question on Stackoverflow
Solution 1 - JavaMathias DpunktView Answer on Stackoverflow
Solution 2 - JavaPierre HenryView Answer on Stackoverflow
Solution 3 - JavaCompitoView Answer on Stackoverflow
Solution 4 - JavaaSemyView Answer on Stackoverflow
Solution 5 - JavaJohnWView Answer on Stackoverflow
Solution 6 - JavaBojan VukasovicView Answer on Stackoverflow
Solution 7 - JavaMatzeView Answer on Stackoverflow
Solution 8 - JavaDemelView Answer on Stackoverflow
Solution 9 - JavaEduardoView Answer on Stackoverflow
Solution 10 - JavaValtoni BoaventuraView Answer on Stackoverflow
Solution 11 - JavaRajan MishraView Answer on Stackoverflow
Solution 12 - JavaGrigory KislinView Answer on Stackoverflow
Solution 13 - JavaMichael KnopfView Answer on Stackoverflow
Solution 14 - JavaBishal JaiswalView Answer on Stackoverflow