无法为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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...