Disable @EnableScheduling on Spring Tests

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

    In each Test you define which spring configuration should be used, currently you have:

    @ContextConfiguration(classes={AppConfiguration.class})
    

    Common practice is to define separate spring configuration for your normal application and for your tests.

    AppConfiguration.java 
    TestConfiguration.java
    

    Then in your test you simply refference TestConfiguration instead of your current AppConfiguration using @ContextConfiguration(classes={TestConfiguration.class})

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes={TestConfiguration.class})
    @Transactional
    @TransactionConfiguration(defaultRollback = true)
    @WebAppConfiguration
    public class ExampleDaoTest
    

    This way you can configure any setting for your tests differently than in production code. You can for example use in-memory database for your tests instead of regular one and much more.

提交回复
热议问题