Last active
June 10, 2019 15:28
-
-
Save timjb/e0ca898f5c248a9533917235647059ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// adapted from https://stackoverflow.com/a/28345802 | |
import org.junit.rules.TestRule; | |
import org.junit.runner.Description; | |
import org.junit.runners.model.Statement; | |
public class RepeatRule implements TestRule { | |
private static class RepeatStatement extends Statement { | |
private final Statement statement; | |
private final int repeat; | |
public RepeatStatement(Statement statement, int repeat) { | |
this.statement = statement; | |
this.repeat = repeat; | |
} | |
@Override | |
public void evaluate() throws Throwable { | |
for (int i = 0; i < repeat; i++) { | |
statement.evaluate(); | |
} | |
} | |
} | |
private int repetitions = 1; | |
public RepeatRule() { | |
super(); | |
} | |
public RepeatRule(int repetitions) { | |
super(); | |
this.repetitions = repetitions; | |
} | |
@Override | |
public Statement apply(Statement statement, Description description) { | |
if (repetitions != 1) { | |
return new RepeatStatement(statement, repetitions); | |
} | |
return statement; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment