Disable @EnableScheduling on Spring Tests

后端 未结 7 874
天涯浪人
天涯浪人 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:26

    I just parameterized my @Scheduled annotation with configurable delay times:

    @Scheduled(fixedRateString = "${timing.updateData}", initialDelayString = "${timing.initialDelay}")
    

    In my test application.yaml:

    timing:
        updateData: 60000
        initialDelay: 10000000000
    

    And main application.yaml:

    timing:
        updateData: 60000
        initialDelay: 1
    

    It's not turning it off but creating such a long delay, the tests will be long over before it runs. Not the most elegant solution but definitely one of the easiest I've found.

提交回复
热议问题