0

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

Don Who
  • 21
  • 5
  • Where is the code where you use the VM argument MODE? And what is the command you execute to run the example? – Nick ten Veen Sep 29 '15 at 09:30
  • I am suppling the VM argument with DMODE=UNIT. I have resolved the Could not find or load main class UNIT. the problem now is that I can't seem to get a set of classes annotated with @UnitTest using the reflection. – Don Who Sep 29 '15 at 09:34
  • First, what is the class 'Reflections'? Second, are you sure the classes annotated with your annotation are on the Classpath as you execute your code? – Michal Sep 29 '15 at 09:44
  • @Michal according to the link I provided ::https://github.com/ronmamo/reflections, I think it gets the class path from the constructor by passing in the package to your unit tests Reflections reflections = new Reflections("my.package"); Is there something I am missing? – Don Who Sep 29 '15 at 09:51
  • You can test if these classes are on your classpath by using `Class.forName("com.drWho.myUnitTests.SOME_CLASS")`, where you specify a concrete unit test class for `SOME_CLASS` – Nick ten Veen Sep 29 '15 at 09:54
  • @NicktenVeen Ok, please, am I missing something from the blog. Isn't providing the package name sufficient for the reflection to get the annotated type? Actually, the reason, I avoided using a classLoader was due to this intuition. Isn't searching my classpaths from the package provided in the constructor? Am I correct on this assertion according to the blog? – Don Who Sep 29 '15 at 10:03
  • @Don you are using the library correctly and that was not where I was going at. I suggested you try loading a unit test class manually to verify that the classes you are trying to find are actually on your classpath. – Nick ten Veen Sep 29 '15 at 11:16
  • @NicktenVeen, unfortunately, when manually tried to verify the classes it failed. This is strange. the class is in com.drWho.myUnitTests.PersonUnitTest and yet running Class.forName("com.drWho.myUnitTests.PersonUnitTest"); throws a ClassNotFoundException. – Don Who Sep 29 '15 at 11:27
  • @NicktenVeen Ok. Seems, using Maven on IntelliJ might be the problem. When using Class.forName() on a unit test class in the same package as my customRunner, it was found but when passing in the package to a class not in the same package with customRunner, the unit classes are not found. Some article claims it is some issue with IDEs and Maven http://codegur.com/13576665/unit-test-using-the-reflections-google-library-fails-only-when-executed-by-maven – Don Who Sep 29 '15 at 11:32
  • There is your problem, they are not on your classpath. Where are the compiled classes located on your filesystem? You need to add them to your classpath so the VM knows of its existence, you can do so by adding the `-cp PATH_TO_COMPILED_CLASSES`, so for example: `java -cp SOME_PATH MainClass -DMODE=UNIT`. Here you can read about classpaths: http://stackoverflow.com/questions/2396493/what-is-a-classpath-confused-and-need-a-human-touch-to-understand – Nick ten Veen Sep 29 '15 at 11:35
  • @NicktenVeen, thanks. seems I am getting somewhere now. Here is my VM : -ea -cp C:\Workspace\Load\testLoad\target\test-classes -DMODE=UNIT Unfortunately, complaining about "Could not find or load main class com.intellij.rt.execution.junit.JUnitStarter" – Don Who Sep 29 '15 at 11:44
  • Ah you are running it with Intellij. Then instead of using command line arguments you can also add the folder to your classpath in the project settings (File -> Project Structure -> Dependencies tab) – Nick ten Veen Sep 29 '15 at 11:54
  • @NicktenVeen, I think it might be there already in the form of Test output path. Isn't there where you add the classpath in the project settings? – Don Who Sep 29 '15 at 12:34
  • No that is where the compiled versions of your code will be stored in. If you want to have the classes on your classpath during testing, you need to go to the dependencies tab, add the folder where the sources are located in and set the scope to Test – Nick ten Veen Sep 29 '15 at 12:44
  • @NicktenVeen, thanks. I did it differently but your figuring out it was the classpath put me in perspective. – Don Who Sep 29 '15 at 13:36

0 Answers0