javaawaitility

How to have meaningful awaitility failure?


I would like be able to receive meaningful errors, for example

Set set = new HashSet();
await().until(() -> set.contains("foo"));
// during waiting
set.add("bar")
// after timeout
"Condition 'foo' should be in set=['bar'] not fulfilled

I can write

await().alias("'foo' should be in set").until(() -> set.contains("foo"));

and get

"Condition with alias 'foo' should be in set not fulfilled

But I would like to see set content when condition was timeouted


Solution

  • It should be possible by setting a conditionEvaluationListener (awaitility/awaitility#102):

    await()
        .conditionEvaluationListener(new ConditionEvaluationListener() {
          @Override public void conditionEvaluated(final EvaluatedCondition condition) {}
          @Override public void onTimeout(final TimeoutEvent event) {
            System.error.println("Timeout waiting for 'foo'. Set contains: " + set);
          }
        })
        .util(() -> set.contains("foo"));
    

    You might also find this old Google Groups thread helpful: Custom messages or method calls on success and timeout