Spring profiles and testing

前端 未结 5 1876
耶瑟儿~
耶瑟儿~ 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 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
    

提交回复
热议问题