summaryrefslogtreecommitdiff
path: root/src/test/java/org/mockitoutil/RetryRule.java
blob: 386ce7800961c287076e54de8b4f6f07c56f8abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */

package org.mockitoutil;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import static java.lang.String.format;

public class RetryRule implements TestRule {
    private final TestRule innerRule;

    public static RetryRule attempts(final int attempts) {
        return new RetryRule(new NumberedAttempts(attempts));
    }

    private RetryRule(TestRule innerRule) {
        this.innerRule = innerRule;
    }

    public Statement apply(final Statement base, final Description description) {
        return innerRule.apply(base, description);
    }

    private static class NumberedAttempts implements TestRule {
        private final int attempts;

        NumberedAttempts(int attempts) {
            assert attempts > 1;
            this.attempts = attempts;
        }

        @Override
        public Statement apply(final Statement base, final Description description) {
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {
                    for (int remainingAttempts = attempts; remainingAttempts > 0 ; remainingAttempts--) {
                        try {
                            base.evaluate();
                        } catch (Throwable throwable) {
                            if (remainingAttempts < 0) {
                                throw new AssertionError(format("Tried this test + %d times and failed", attempts))
                                        .initCause(throwable);
                            }
                        }
                    }
                }
            };
        }
    }
}