Spring profiles and testing

前端 未结 5 1842
耶瑟儿~
耶瑟儿~ 2020-12-24 10:36

I\'ve got a web application where I have the typical problem that it requires different configuration files for different environments. Some configuration is placed in the a

相关标签:
5条回答
  • 2020-12-24 10:57

    @EnableConfigurationProperties needs to be there (you also can annotate your test class), the application-localtest.yml from test/resources will be loaded. A sample with jUnit5

    @ExtendWith(SpringExtension.class)
    @EnableConfigurationProperties
    @ContextConfiguration(classes = {YourClasses}, initializers = ConfigFileApplicationContextInitializer.class)
    @ActiveProfiles(profiles = "localtest")
    class TestActiveProfile {
    
        @Test
        void testActiveProfile(){
    
        }
    }
    
    0 讨论(0)
  • 2020-12-24 10:58

    Looking at Biju's answer I found a working solution.

    I created an extra context-file test-context.xml:

    <context:property-placeholder location="classpath:config/spring-test.properties"/>
    

    Containing the profile:

    spring.profiles.active=localtest
    

    And loading the test with:

    @RunWith(SpringJUnit4ClassRunner.class)
    @TestExecutionListeners({
        TestPreperationExecutionListener.class
        })
    @Transactional
    @ActiveProfiles(profiles = "localtest")
    @ContextConfiguration(locations = {
        "classpath:config/test-context.xml" })
    public class TestContext {
    
      @Test
      public void testContext(){
    
      }
    }
    

    This saves some work when creating multiple test-cases.

    0 讨论(0)
  • 2020-12-24 11:02
    public class LoginTest extends BaseTest {
        @Test
        public void exampleTest( ){ 
            // Test
        }
    }
    

    Inherits from a base test class (this example is testng rather than jUnit, but the ActiveProfiles is the same):

    @ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
    @ActiveProfiles(resolver = MyActiveProfileResolver.class)
    public class BaseTest extends AbstractTestNGSpringContextTests { }
    

    MyActiveProfileResolver can contain any logic required to determine which profile to use:

    public class MyActiveProfileResolver implements ActiveProfilesResolver {
        @Override
        public String[] resolve(Class<?> aClass) {
            // This can contain any custom logic to determine which profiles to use
            return new String[] { "exampleProfile" };
        }
    }
    

    This sets the profile which is then used to resolve dependencies required by the test.

    0 讨论(0)
  • 2020-12-24 11:03

    The best approach here is to remove @ActiveProfiles annotation and do the following:

    @RunWith(SpringJUnit4ClassRunner.class)
    @TestExecutionListeners({
        TestPreperationExecutionListener.class
        })
    @Transactional
    @ContextConfiguration(locations = {
        "classpath:config/test-context.xml" })
    public class TestContext {
    
      @BeforeClass
      public static void setSystemProperty() {
            Properties properties = System.getProperties();
            properties.setProperty("spring.profiles.active", "localtest");
      }
    
      @AfterClass
      public static void unsetSystemProperty() {
            System.clearProperty("spring.profiles.active");
      }
    
      @Test
      public void testContext(){
    
      }
    }
    

    And your test-context.xml should have the following:

    <context:property-placeholder 
      location="classpath:META-INF/spring/config_${spring.profiles.active}.properties"/>
    
    0 讨论(0)
  • 2020-12-24 11:20

    Can I recommend doing it this way, define your test like this:

    @RunWith(SpringJUnit4ClassRunner.class)
    @TestExecutionListeners({
        TestPreperationExecutionListener.class
        })
    @Transactional
    @ActiveProfiles(profiles = "localtest")
    @ContextConfiguration
    public class TestContext {
    
      @Test
      public void testContext(){
    
      }
    
      @Configuration
      @PropertySource("classpath:/myprops.properties")
      @ImportResource({"classpath:context.xml" })
      public static class MyContextConfiguration{
    
      }
    }
    

    with the following content in myprops.properties file:

    spring.profiles.active=localtest
    

    With this your second properties file should get resolved:

    META-INF/spring/config_${spring.profiles.active}.properties
    
    0 讨论(0)
提交回复
热议问题