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

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

提交回复
热议问题