springjunitmockitospring-scheduledshedlock

Unable to write unit test for Spring @Scheduler


I have implemented a scheduler with shedlock in my current spring project as follows:

  @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();
    ...
  }

Now I would like to write unit test for this function. Here the problem I am facing is: I am unable to mock first statement: LockAssert.assertLocked().


Solution

  • This should do the trick LockAssert.TestHelper.makeAllAssertsPass(true);

    Just add it at the beginning of the test method.

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