mardi 23 avril 2019

How to get the list of all @Test annotation methods from test source package

I'm working on a project where in all the test classes in a package will populate in UI where in I can select any of the class and run. Now I want to extend it to test methods run. For this I need to retrieve all test methods in a test class.I found following class which will give me test classes.But the problem is ,i am easily getting all @Test methods from src/main/java packages ,not from src/test/java.it can't recognize the test source package.

I have applied the below code and it can give me all class names and method names which contains @Test methods.But it is not applicable to find out the class & method names from src/test/java of maven project.

public static Set> getClassesFromPackage(String packageName) { Reflections reflections = new Reflections(packageName, new SubTypesScanner(false)); return reflections.getSubTypesOf(Object.class); }

public static void main(String[] args) {
    for ( Class c: getClassesFromPackage("SourceMainJavaPackageName" )){

            Method[] methods = c.getMethods();
            for (Method method : methods) {                 
                for (Annotation annotation : method.getDeclaredAnnotations()) {
                    if (annotation.annotationType().equals(org.junit.Test.class)) {
                        count++;
                    }
                }
            }               

               }
}

i want @Test method names and corresponding class names from src/test/java.

Aucun commentaire:

Enregistrer un commentaire