简体   繁体   中英

Is there any way to find @Disabled Tests in a class of Java test class in Junit5?

Is there any way to find @Disabled Tests in a class of Java test class in Junit5 ? I am using junit.platform.launcher to discover test cases in my project. I am creating a TestPlan and trying to find the tests which are disabled but cant see anything there to help me.

Disabling tests is an execution time feature since disabling/enabling may depend on stuff that's only available during execution. That's why the test identifiers in the test plan don't show it.

All you can do is check for the annotation being present on a method or a container class. Mind that the annotation can be „meta present“ ie the annotation of another annotation. Some methods in https://junit.org/junit5/docs/current/api/org.junit.platform.commons/org/junit/platform/commons/support/AnnotationSupport.html are probably useful to reduce the amount of boilerplate code to detect all cases.

I found the solution by using Reflection and annotations class to get the annotations. Once I received the list of annotations, I used a if condition to check if there is any Disabled tag present in the test case or not.

`Class clazz = Class.forName (className);
    Method[] m = clazz.getDeclaredMethods ();
    for(Method method : m){
        if(method.getName ().equalsIgnoreCase (methodName)) {
            Annotation[] annotations = method.getAnnotations ();
            for(Annotation anno : annotations) {
                if(anno.annotationType ().getName ().equalsIgnore("org.junit.jupiter.api.Disabled")){
                    // Do Something
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM