【问题标题】:Testing Java Spring @Scheduled functionality [duplicate]测试 Java Spring @Scheduled 功能 [重复]
【发布时间】:2020-05-13 20:52:05
【问题描述】:

我有如下的 cron 调度程序:

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

我正在尝试测试此预定方法的全部功能 - 某种集成测试(包括方法执行的正确性 - 时间)。 你有什么建议我该怎么做?

【问题讨论】:

标签: java spring spring-boot


【解决方案1】:

您可以使用为此目的提供的 Awaility 库:

使用 maven 添加该库:

<dependency>
    <groupId>org.awaitility</groupId>
    <artifactId>awaitility</artifactId>
    <version>3.1.6</version>
    <scope>test</scope>
</dependency>

我们可以使用 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

【讨论】:

猜你喜欢
  • 2021-04-30
  • 2010-10-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 2015-11-09
  • 2020-05-23
  • 2013-01-02
  • 1970-01-01
相关资源
最近更新 更多