【问题标题】:How to assert that condition is fulfilled eventually?如何断言条件最终满足?
【发布时间】:2018-11-27 16:53:03
【问题描述】:

我正在尝试编写一个 JUnit5 测试,断言最终在接下来的 15 秒内满足条件。我怎样才能最好地实现它?

我正在考虑这个问题:

    assertTimeout(ofSeconds(15), () -> {assertThat(condition, is(true));});

但它应该反复测试条件

【问题讨论】:

  • 超出范围:您知道,这样的测试是最终导致 CI​​ 失败的秘诀。更好的是在没有任何计时器功能的情况下测试事件。但我不知道你的用例,我只是建议记住这一点。

标签: java junit junit5


【解决方案1】:

看看Awaitility,它具有您正在寻找的功能。 (docs here)

@Test
public void updatesCustomerStatus() throws Exception {

  // Publish an asynchronous event:
  publishEvent(updateCustomerStatusEvent);
  // Awaitility lets you wait until the asynchronous operation completes:
  await().atMost(5, SECONDS).until(customerStatusIsUpdated());
  ...
}

【讨论】:

    猜你喜欢
    • 2022-10-05
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2017-08-01
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多