I have the following code. It works on loading the class I pointed which is TestSuiteSample. TestSuiteSample is just a standard JUnit4 Test Suite with annotations. Running as normal java project this will print “true” but running under eclipse rcp this will print “false”. Any ideas why?
File testSuiteDir = new File("D:/TestDir/");
URL classUrl;
classUrl = testSuiteDir.toURI().toURL();
URL[] classUrls = { classUrl };
URLClassLoader ucl = new URLClassLoader(classUrls);
Class<?> cls = ucl.loadClass("TestSuiteSample");
if (cls.isAnnotationPresent(SuiteClasses.class)) {
System.out.println("true");
} else {
System.out.println("false");
}
I tried this one to check for annotations in cls, but still returns 0 annotations.
Annotation[] annotations = new TestClass(clazz).getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation.annotationType().getName());
}
The problem is the
SuiteClassesclass is coming from an unrelatedClassLoaderthan from theURLClassLoaderyou are using to load the class you are testing. If you make the parent of theURLClassLoaderthe bundle or thread contextClassLoaderthen it should resolve to the same class.