【问题标题】:Unable to write unit test for Spring @Scheduler无法为 Spring @Scheduler 编写单元测试
【发布时间】:2020-12-02 20:38:40
【问题描述】:

我在当前的 spring 项目中实现了一个带锁的调度器,如下所示:

  @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()

【问题讨论】:

    标签: spring junit mockito spring-scheduled shedlock


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 2019-11-19
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多