Java Spring Boot Test: How to exclude java configuration class from test context

前端 未结 5 771
后悔当初
后悔当初 2021-01-17 07:41

I have a Java web app with spring boot

When run test I need to exclude some Java config files:

Test config (need to include when test run):

@         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-01-17 08:37

    You can also use @ConditionalOnProperty like below:

    @ConditionalOnProperty(value="otpConfig", havingValue="production")
    @Configuration
    @PropertySource("classpath:otp.properties")
    public class OTPConfig { }
    

    and for test:

    @ConditionalOnProperty(value="otpConfig", havingValue="test")
    @Configuration
    @PropertySource("classpath:otp-test.properties")
    public class TestOTPConfig { }
    

    Than specify in your main/resources/config/application.yml

    otpConfig: production
    

    and in your test/resources/config/application.yml

    otpConfig: test
    

提交回复
热议问题