Reputation: 567
I want to run a simple Rule that will try to rerun the test if it has a specific annotation.Specifically,it'll try to rerun the test as many times as the user wants,by reading the tries int from a cusotm annotationt.
Here is the cusotm annotation i created: import java.lang.annotation.ElementType; import java.lang.annotation.Target;
@Target(ElementType.METHOD)
public @interface Retry {
int tries();
}
And here is the custom rule i created:
public class RetryRule implements TestRule {
@Override
public Statement apply(final Statement base, final Description description) {
if( description.getAnnotation(Retry.class) == null ) {
System.out.println("Hm...");
return base;
}
int tries = description.getAnnotation(Retry.class).tries();
return new Statement() {
@Override
public void evaluate() throws Throwable {
while(true) {
int i = 1 ;
try {
i++;
base.evaluate();
return ;
} catch (AssertionError ex) {
if( i >= tries ) {
return ;
}
}
}
}
};
}
}
The problem is that whenever i run a test with the custom annotation and the custom rule,it always runs only once.I checked,and the getAnnotation(....) returns always null in any case — if cusotm annotation is specified or no.
Upvotes: 0
Views: 334
Reputation: 25976
Your annotation is not retained till runtime.
Use @Retention(RetentionPolicy.RUNTIME)
Upvotes: 4