测试Java Spring的@Scheduled功能

3

我有一个由cron作为调度程序的计划:

@Scheduled(cron = "0 0 1 * * *")
@SchedulerLock(name = "PT20M")
public void schedulerSubscriptionsUpdate() {}

我正在尝试测试此计划方法的全部功能-某种集成测试(包括方法执行的正确性-时间)。 你有什么建议可以帮助我实现这个目标吗?


2
这个回答解决了你的问题吗?如何测试Spring @Scheduled - Daniel
https://www.baeldung.com/spring-testing-scheduled-annotation - Prashant
@nodlaw,你解决了这个测试吗? - elmigue017
1个回答

1
你可以使用 Awaility 库来实现这个目的:
通过 Maven 添加该库:
<dependency>
    <groupId>org.awaitility</groupId>
    <artifactId>awaitility</artifactId>
    <version>3.1.6</version>
    <scope>test</scope>
</dependency>

我们可以使用Awaitility DSL使我们的测试更具声明性,例如:

我们可以使用Awaitility DSL使我们的测试更具声明性,例如:

@SpringJUnitConfig(ScheduledConfig.class)
public class ScheduledAwaitilityIntegrationTest {

    @SpyBean
    private Counter counter;

    @Test
    public void whenWaitOneSecond_thenScheduledIsCalledAtLeastTenTimes() {
        await()
          .atMost(Duration.ONE_SECOND)
          .untilAsserted(() -> verify(counter, atLeast(10)).scheduled());
    }
}

来源 - baeldung.com/spring-testing-scheduled-annotation

这篇文章介绍了如何使用Spring Testing框架对带有@Scheduled注解的方法进行测试。


这是从https://www.baeldung.com/spring-testing-scheduled-annotation复制并粘贴的内容。 - elmigue017
@elmigue017 - 有时候我们不得不这样做,这是我们的错。 - Pramod S. Nikam
@elmigue017 - 在答案中添加了源代码 - Pramod S. Nikam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接