Spring-boot default profile for integration tests

后端 未结 11 1043
说谎
说谎 2020-12-07 14:49

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 co

相关标签:
11条回答
  • 2020-12-07 14:51

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

        test.doFirst {
            systemProperty 'spring.profiles.active', 'test'
            activeProfiles = 'test'
        }
    
    0 讨论(0)
  • 2020-12-07 14:51

    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

    0 讨论(0)
  • 2020-12-07 14:53

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

    0 讨论(0)
  • 2020-12-07 14:53

    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=localbuild/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

    0 讨论(0)
  • 2020-12-07 14:57

    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.

    0 讨论(0)
  • 2020-12-07 14:59

    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.
    0 讨论(0)
提交回复
热议问题