Disable @EnableScheduling on Spring Tests

后端 未结 7 852
天涯浪人
天涯浪人 2020-11-30 02:35

When I run my unit tests, it invokes my scheduled tasks. I want to prevent this behaviour, which is caused by the fact that I have @EnableScheduling on my main

7条回答
  •  一向
    一向 (楼主)
    2020-11-30 03:22

    If you don't want to use profiles, you can add flag that will enable/disable scheduling for the application

    In your AppConfiguration add this

      @ConditionalOnProperty(
         value = "app.scheduling.enable", havingValue = "true", matchIfMissing = true
      )
      @Configuration
      @EnableScheduling
      public static class SchedulingConfiguration {
      }
    

    and in your test just add this annotation to disable scheduling

    @TestPropertySource(properties = "app.scheduling.enable=false")
    

提交回复
热议问题