Skip to content

Commit

Permalink
test: Added convenience RepeatTestRule.
Browse files Browse the repository at this point in the history
  • Loading branch information
kelemen committed Jan 28, 2022
1 parent c3f9e61 commit cfed6bc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jtrim2.testutils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RepeatTest {
public int value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jtrim2.testutils;

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

public final class RepeatTestRule implements TestRule {
@Override
public Statement apply(Statement base, Description description) {
RepeatTest annotation = description.getAnnotation(RepeatTest.class);
if (annotation == null) {
return base;
}
int repeatCount = annotation.value();
if (repeatCount <= 0) {
throw new IllegalStateException("Unexpected repeat count " + repeatCount
+ " on " + description.getDisplayName());
}
return new Statement() {
@Override
public void evaluate() throws Throwable {
for (int i = repeatCount; i > 0; i--) {
base.evaluate();
}
}
};
}
}

0 comments on commit cfed6bc

Please sign in to comment.