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

后端 未结 3 593
爱一瞬间的悲伤
爱一瞬间的悲伤 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:00

    You should use spring boot profiles:

    1. Annotate your test configuration with @Profile("test").
    2. Annotate your production configuration with @Profile("production").
    3. Set production profile in your properties file: spring.profiles.active=production.
    4. Set test profile in your test class with @Profile("test").

    So when your application starts it will use "production" class and when test stars it will use "test" class.

    If you use inner/nested @Configuration class it will be be used instead of a your application’s primary configuration.

    0 讨论(0)
  • 2020-12-04 17:02

    Inner test configuration

    Example of an inner @Configuration for your test:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SomeTest {
    
        @Configuration
        static class ContextConfiguration {
            @Bean
            @Primary //may omit this if this is the only SomeBean defined/visible
            public SomeBean someBean () {
                return new SomeBean();
            }
        }
    
        @Autowired
        private SomeBean someBean;
    
        @Test
        public void testMethod() {
            // test
        }
    }
    

    Reusable test configuration

    If you wish to reuse the Test Configuration for multiple tests, you may define a standalone Configuration class with a Spring Profile @Profile("test"). Then, have your test class activate the profile with @ActiveProfiles("test"). See complete code:

    @RunWith(SpringRunner.class)
    @SpringBootTests
    @ActiveProfiles("test")
    public class SomeTest {
    
        @Autowired
        private SomeBean someBean;
    
        @Test
        public void testMethod() {
            // test
        }
    }
    
    @Configuration
    @Profile("test")
    public class TestConfiguration {
        @Bean
        @Primary //may omit this if this is the only SomeBean defined/visible
        public SomeBean someBean() {
            return new SomeBean();
        }
    }
    

    @Primary

    The @Primary annotation on the bean definition is to ensure that this one will have priority if more than one are found.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题