Fischer Ludrian
Fischer Ludrian

Reputation: 677

How to assert that condition is fulfilled eventually?

I'm trying to write a JUnit5 tests that asserts that a conditions is fullfilled eventually in the next 15 seconds. How can I best implement that?

I was thinking something about this:

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

But it should repeatedly test the condition

Upvotes: 9

Views: 7919

Answers (1)

George Artemiou
George Artemiou

Reputation: 3176

Take a look at Awaitility, it has the functionality that you are looking for. (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());
  ...
}

Upvotes: 12

Related Questions