1

Spring boot(2.2.4),

@SpringBootTest
@ActiveProfiles("test")
@Slf4j
class FooServiceImplTest {
    @Test
    void foo(){...}
}

because there is a class(MyJob.class) with Scheduled Annotation

@Scheduled(cron = "0 */1 * * * ?")

so when execute foo test, it will execute the scheduled task, and could see below logger

[2022/05/26 21:21:13] [main] INFO  o.s.s.c.ThreadPoolTaskExecutor  171 - Initializing ExecutorService 'applicationTaskExecutor'
[2022/05/26 21:21:14] [main] INFO  o.s.s.c.ThreadPoolTaskScheduler  171 - Initializing ExecutorService 'taskScheduler'
[2022/05/26 21:21:14] [main] INFO  c.b.p.s.i.FooServiceImplTest  61 - Started FooServiceImplTest in 3.634 seconds (JVM running for 4.332)

[2022/05/26 21:21:14] [SpringContextShutdownHook] INFO  o.s.s.c.ThreadPoolTaskScheduler  208 - Shutting down ExecutorService 'taskScheduler'
[2022/05/26 21:21:14] [SpringContextShutdownHook] INFO  o.s.s.c.ThreadPoolTaskExecutor  208 - Shutting down ExecutorService 'applicationTaskExecutor'

How to ignore scheduled task conveniently? e.g.

@DisableScheduling 
class FooServiceImplTest {

or

@IgnoreClass(MyJob.class)
class FooServiceImplTest {

Now when I execute test method, I manually explicitly comment EnableScheduling first

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
//@EnableScheduling
public class FooApplication {
zhuguowei
  • 8,401
  • 16
  • 70
  • 106

0 Answers0