Override a single @Configuration class on every spring boot @Test

后端 未结 3 604
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 16:19

On my spring boot application I want to override just one of my @Configuration classes with a test configuration (in particular my @EnableAuthorizationSer

3条回答
  •  广开言路
    2020-12-04 17:03

    I recently had to create a dev version of our application, that should run with dev active profile out of the box without any command line args. I solved it with adding this one class as a new entry, which sets the active profile programmatically:

    package ...;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.context.annotation.Import;
    import org.springframework.core.env.ConfigurableEnvironment;
    import org.springframework.core.env.StandardEnvironment;
    
    @Import(OriginalApplication.class)
    public class DevelopmentApplication {
        public static void main(String[] args) {
            SpringApplication application =
                new SpringApplication(DevelopmentApplication.class);
            ConfigurableEnvironment environment = new StandardEnvironment();
            environment.setActiveProfiles("dev");
            application.setEnvironment(environment);
            application.run(args);
        }
    }
    

    See Spring Boot Profiles Example by Arvind Rai for more details.

提交回复
热议问题