我遇到了Reflections的问题。我正在尝试Set
使用方法获取一个字段,Reflections#getFieldsAnnotatedWith
但是当我运行单元测试时,它什么也不返回,有人能告诉我为什么吗?(我正在使用 IntelliJ IDE)
这是我正在使用的课程,它非常基础。
//The test class run with junit
public class ReflectionTestingTest {
@Test
public void test() {
Reflections ref = new Reflections(AnnotatedClass.class);
assertEquals(2, ref.getFieldsAnnotatedWith(TestAnnotation.class).size());
Set<Field> fields = ref.getFieldsAnnotatedWith(TestAnnotation.class);
}
}
//The class with the annotated fields I want to have in my Set.
public class AnnotatedClass {
@TestAnnotation
public int annotatedField1 = 123;
@TestAnnotation
public String annotatedField2 = "roar";
}
//And the @interface itself
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestAnnotation {}
测试失败并显示以下消息:
junit.framework.AssertionFailedError:
Expected :2
Actual :0