Skip to content

Instantly share code, notes, and snippets.

@braghome
Forked from abyx/RetrierTest.java
Last active August 29, 2015 14:22
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 braghome/0411c3c121d49475759d to your computer and use it in GitHub Desktop.
Save braghome/0411c3c121d49475759d to your computer and use it in GitHub Desktop.
public class RetrierTest {
private static int count = 0;
@Rule public RetryRule rule = new RetryRule();
@Test
@Retry
public void failsFirst() throws Exception {
count++;
assertEquals(2, count);
}
}
@Retention(RetentionPolicy.RUNTIME)
public @interface Retry {}
public class RetryRule implements MethodRule {
@Override public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
return new Statement() {
@Override public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (Throwable t) {
Retry retry = method.getAnnotation(Retry.class);
if (retry != null) {
base.evaluate();
} else {
throw t;
}
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment