使用反射从类中获取注释

时间:2015-09-29 09:16:21

标签: java junit-runner

我正在尝试使用此博客中的反射示例:https://github.com/ronmamo/reflections

获取我的junit代码中使用@unitTests注释的类列表。

以下是我所做的:

//imports here...
@RunWith(CustomRunner.UnitRunner.class)
public class CustomRunner {

private CustomRunner{}
public static class UnitRunner extends Suite {
        public UnitRunner(final Class<?> clazz) throws InitializationError {
            super(clazz, findClasses());
        }

 private static Class<?>[] findClasses() {
            if (System.getProperty(MODE_VM_ARG) == null) {
                String message = "Please supply run mode VM arg -DMODE=...";
                JOptionPane.showMessageDialog(null, message);
                System.exit(-1);
            }
            List<File> classFiles = new ArrayList<File>();
            List<Class<?>> classes = convertToClasses(classFiles);

private static List<Class<?>> convertToClasses(final List<File> classFiles) {
Reflections reflections = new Reflections("com.drWho.myUnitTests");
     Set<Class<?>> annotatedWithUnitTest = reflections.getTypesAnnotatedWith(UnitTest.class);
//...more other stuffs down here..
}

}
//class ends some where....doesn't really matter


****//my unitTest annotated class****



package com.com.drWho.utils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface UnitTest {

}

反射似乎无法从构造函数中提供的包中找到单元测试类。不知道为什么。有什么我做错了吗?

由于

0 个答案:

没有答案