'Test Spring scheduled tasks with Awaitility and Kotlin

I have created a bean to run a cron based task.

and write a test to ensure it is working as expected.


@SpringBootTest(properties = ["tasks.mytask.cron=0/5 * * * * *"])
class ScheduledTasksTest {
    companion object {
        private val log = LoggerFactory.getLogger(ScheduledTasksTest::class.java)
    }

    @SpykBean
    lateinit var tasks: ScheduledTasks

    @Test
    fun `verify task mytask at least run twice in 10 seconds`() {
        await atMost (Duration.ofSeconds(10)) untilAsserted {
            verify(atLeast = 2) { tasks.mytask () }
        }
    }

}

This task is set to run once per day, but in this test, I changed it to run every 5 seconds.

The test is running well on my local machine, but when I run all tests on Github Actions, the task is always running (with every 5 seconds rate) and never stop.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source