Spring-boot default profile for integration tests

Piotr Zakrzewski :

Spring-boot utilizes Spring profiles (http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html) 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

Mathias Dpunkt :

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"};
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=419679&siteId=1