Skip to content

Instantly share code, notes, and snippets.

@timjb
Last active June 10, 2019 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timjb/e0ca898f5c248a9533917235647059ec to your computer and use it in GitHub Desktop.
Save timjb/e0ca898f5c248a9533917235647059ec to your computer and use it in GitHub Desktop.
// 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