I am trying to use the reflection example from this blog:https://github.com/ronmamo/reflections
to get a list of classes annotated with the @unitTests in my junit code.
Below is what I have done:
//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 {
}
The reflection can't seem to locate the unit test classes from the package supplied in the constructor. don't know why. Is there something I am doing wrong?
Thanks