Spring-boot default profile for integration tests

Spring-boot default profile for integration tests

spring boot test profile
spring boot integration test
spring boot test exclude configuration
spring boot test autowired null
spring boot test configuration
spring integration test
spring-test profile
spring boot unit test profile

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


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

Integration Tests with @SpringBootTest, Before we start into integration tests with Spring Boot, let's define what sets @​SpringBootTest by default starts searching in the current package of the test and load the properties from that file by activating a certain profile:. The Spring Framework provides first-class support for integration testing in the spring-test module. The name of the actual JAR file might include the release version and might also be in the long org.springframework.test form, depending on where you get it from (see the section on Dependency Management for an explanation).


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).

Setting default Spring profile for tests with override option, Setting default Spring profile for tests with override option One of areas where profiles can also be particularly useful are integration tests. When spring boots up application context without explicitly set profiles it reports an  Before we start into integration tests with Spring Boot, let’s define what sets an integration test apart from a unit test. A unit test covers a single “unit”, where a unit commonly is a single class, but can also be a cluster of cohesive classes that is tested in combination. An integration test can be any of the following:


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.

14. Integration Testing - Project Metadata API Guide, By default, the framework will create and roll back a transaction for each test. See the section called “Context configuration with environment profiles” and the integration testing scenario for a Spring MVC web application by declaring a  Unit and Integration Tests in Spring Boot A well-written Spring application should be easy to test with a clean separation of concerns. Look at a guide on how to do quality tests in Spring Boot.


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.

Spring 3.1 M2: Testing with @Configuration Classes and Profiles, By default the ApplicationContext is loaded using the GenericXmlContextLoader which Integration Testing with XML-based Configuration. Traditionally, you will name your unit tests with the suffix of ‘Test’ or ‘Tests’, and your integration tests with the suffix of ‘IT’. This does not affect how JUnit runs the tests. But it does have ramifications later when building with tools such as Maven and deploying and reporting from Continuous Build servers such as Jenkins.


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

11. Integration Testing - Project Metadata API Guide, Support for the caching of loaded contexts is important, because startup time By default, the framework will create and roll back a transaction for each test. See the section called “Context configuration with environment profiles” and the  An important thing to notice about integration testing is that test methods often take quite a long time to run. As a result, we should exclude integration tests from the default build lifecycle, keeping them from slowing down the whole process each time we build a project. A convenient way to separate integration tests is to use build profiles.


@ActiveProfiles Example in Spring Test, The default value is true. @ActiveProfiles are used in Spring integration test as following. To Activate multiple profiles using @ActiveProfiles , specify profiles Technologies Used; Using @ActiveProfiles; @ActiveProfiles in Inheritance; @​ActiveProfiles in Spring Boot; References; Download Source Code  The spring-boot-starter-test is the primary dependency that contains the majority of elements required for our tests. The H2 DB is our in-memory database. It eliminates the need for configuring and starting an actual database for test purposes.


Optimizing Spring Integration Tests, In this article, we'll have a holistic discussion about integration tests using Spring and how to optimize them. Creating application contexts might be snappy with a vanilla spring boot app with nothing in it. Add a bunch of profiles, and scatter them through a few tests and we'll set default requests if any. Dev/test is confusing in your answer. I'd expect a prod profile and then a test profile but not a "Dev" one. You could just as well have a "prod" profile with the production and leave the default for the test. Since Spring Boot does not find any specific information it'll use the embedded container. – Stephane Nicoll Aug 14 '15 at 6:33


Spring Profiles, Configuring Separate Spring DataSource for Tests. A quick, practical Properties with Spring and Spring Boot. Tutorial for how to work The Default Profile. Any bean that does not specify a profile belongs to “default” profile. Using @Profiles in springboot src/test/* folder. The @ActiveProfiles is actually activated (when viewing logs), but it doesn't use the @Profile when executed with gradle test. However, the @Profile was working in Eclipse Editor when JUnit test was executed. So in the end i found that @Profile needs to be in src/main/* folder. Gradle test seems