Spring profiles and testing

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

提交回复
热议问题