无法为Spring @Scheduler编写单元测试

问题描述

我在当前的春季项目中实现了带有挂锁的调度程序,如下所示:

  @Scheduled(cron = "0 0/1 * * * *")
  @SchedulerLock(name = "syncData",lockAtMostFor = "${shedlock.myScheduler.lockAtMostFor}",lockAtLeastFor = "${shedlock.myScheduler.lockAtLeastFor}")
  public void syncSAData() {
    //To assert that the lock is held
    LockAssert.assertLocked();
    ...
  }

现在,我想为此功能编写单元测试。这里我面临的问题是:我无法模拟第一条陈述:LockAssert.assertLocked()

解决方法

这应该可以解决问题LockAssert.TestHelper.makeAllAssertsPass(true);

只需将其添加到测试方法的开头即可。

文档:https://github.com/lukas-krecan/ShedLock#lock-assert