1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
public class TestAnnotations {
@Rule
public MethodRule detecterMonAnnotation = new AfficherTexteJunitRule();
@Test
@AfficherTexte("mon texte")
public void testAvecAnnotation() {
System.out.println("test : testAvecAnnotation");
}
@Test
public void testSansAnnotation() {
System.out.println("test : testSansAnnotation");
}
public class AfficherTexteJunitRule implements MethodRule {
@Override
public Statement apply(Statement base, FrameworkMethod frameworkMethod, Object target) {
Method method = frameworkMethod.getMethod();
AfficherTexte afficherTexte = method.getAnnotation(AfficherTexte.class);
if (afficherTexte != null) {
System.out.println(afficherTexte.value());
}
return base;
}
}
@Retention(RetentionPolicy.RUNTIME)
public @interface AfficherTexte {
String value() default "";
}
} |
Partager