Disable @EnableScheduling on Spring Tests

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

    One more solution without any change in production code, using the @MockBean.

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @MockBean(MyScheduledClass.class)
    public class MyTest {
    

    Which will eventually replace active scheduled job or create a mocked one.

    From the documentation

    Mocks can be registered by type or by {@link #name() bean name}. Any existing single bean of the same type defined in the context will be replaced by the mock, if no existing bean is defined a new one will be added.

提交回复
热议问题