Disable @EnableScheduling on Spring Tests

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

    An alternative would be to unregister the bean post processor that schedules the events. This can be done by simply putting the following class on the classpath of your tests:

    public class UnregisterScheduledProcessor implements BeanFactoryPostProcessor {
    
        @Override
        public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
            for (String beanName : beanFactory.getBeanNamesForType(ScheduledAnnotationBeanPostProcessor.class)) {
                ((DefaultListableBeanFactory)beanFactory).removeBeanDefinition(beanName);
            }
        }
    }
    

    While this is quite simple and seems to do the job, beware that I did not test this very much or check for possible implications of removing a defined bean from the registry or making sure that ordering of PostProcessors won't be an issue...

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