lundi 26 mars 2018

Java Reflections - Getting method with a specific type of annotation with Reflections

i'm trying to get method with annotation @Before from MyTestClass.class in package named ru.package. It works fine when i have only 1 class with @Before. But if i create new classes and new classes in inner packages with this annotation, this code find them too. Where did I go wrong? Thanks.

Reflections reflections = new Reflections(new ConfigurationBuilder().
                setUrls(ClasspathHelper.forClass(MyTestClass.class)).
                setScanners(new MethodAnnotationsScanner()));
Set<Method> methods = reflections.getMethodsAnnotatedWith(Before.class);

@Before annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Before {
}

MyTestClass.class

public class MyTestClass {

    private List<String> stringList = new ArrayList<>();

    @Before
    public void setUp() {
        stringList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            stringList.add("string " + i);
        }
    }
    //@Test and etc
}





Aucun commentaire:

Enregistrer un commentaire