Skip to content
Advertisement

How to avoid repetition with same params

I am trying to find out a way to avoid the kind of repetition below

try {
    await().pollInterval(5, TimeUnit.SECONDS).pollDelay(500, TimeUnit.MILLISECONDS) 
        .atMost(30, TimeUnit.SECONDS).until(() -> {
            // 1. methods in here
        });
} catch (final Exception e) {
    //TODO: can be handled internally
}

It happens in several places in my code, and I want to make it less repetitive, but I am not finding a way to do so. I thought about lambdas, but I don`t know much about it nor if it would fit in here.

Inside of it can be many different things, it is not the same for all nor they have the same inheritance.

Advertisement

Answer

public static void main(String... args) {
    awaitUntil(() -> {
        // payload 1
        return true;
    });

    awaitUntil(() -> {
        // payload 2
        return true;
    });
}

public static void awaitUntil(Callable<Boolean> conditionEvaluator) {
    try {
        Awaitility.await()
                  .pollInterval(5, TimeUnit.SECONDS)
                  .pollDelay(500, TimeUnit.MILLISECONDS)
                  .atMost(30, TimeUnit.SECONDS)
                  .until(conditionEvaluator);
    } catch(Exception e) {
        //TODO: can be handled internally
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement