Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Retry implements TestRule {
- private int retryCount = 10;
- @Override
- public Statement apply(Statement base, Description description) {
- return new Statement() {
- public void evaluate() throws Throwable {
- RetryCount annotation = description.getAnnotation(RetryCount.class);
- // Problem is here, the annotation is always null!
- int retries = (annotation != null) ? annotation.retries() : retryCount;
- // keep track of the last failure to include it in our failure later
- AssertionError lastFailure = null;
- for (int i = 0; i < retries; i++) {
- try {
- // call wrapped statement and return if successful
- base.evaluate();
- return;
- } catch (AssertionError err) {
- lastFailure = err;
- }
- }
- // give meaningful message and include last failure for the
- // error trace
- throw new AssertionError("Gave up after " + retries + " tries", lastFailure);
- }
- };
- }
- // the annotation for method-based retries
- public static @interface RetryCount {
- public int retries() default 1;
- }
- }
- public class UnreliableServiceUnitTest {
- private UnreliableService sut = new UnreliableService();
- @Rule
- public Retry retry = new Retry();
- @Test
- @RetryCount(retries=5) // here it is
- public void worksSometimes() {
- boolean worked = sut.workSometimes();
- assertThat(worked, is(true));
- }
- }
- public class UnreliableService {
- private static Random RANDOM = new Random();
- // needs at least two calls
- private static int COUNTER = RANDOM.nextInt(8) + 2;
- public boolean workSometimes() {
- if (--COUNTER == 0) {
- COUNTER = RANDOM.nextInt(8) + 2;
- return true;
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment