带有静态类列表的Junit SuiteClasses [英] Junit SuiteClasses with a static list of classes

查看:124
本文介绍了带有静态类列表的Junit SuiteClasses的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SuiteClasses可以很好地处理像 {Test1.class,Test2.class} 这样的类列表,但是当我尝试生成类的静态列表时,它说不兼容的类型:必需 java.lang.Class<?> 但找到 java.lang.Class<?> []

SuiteClasses will work just fine with a list of classes like {Test1.class,Test2.class}, but when I try to generate a static list of classes, it says incompatible types: required java.lang.Class<?> but found java.lang.Class<?>[]

我缺少什么?

@RunWith(Suite.class)

@Suite.SuiteClasses(TestSuite.classes)
public class TestSuite {

    public static Class<?> [] classes;

    static {
       classes = new Class<?> [1];
       classes[0] = MyTest.class;
    }
}


推荐答案

那不应该真的有效。您打算将注释作为常量放在注释中。即使你遇到了这个问题,编译器也会拒绝它。你需要做的是:

That shouldn't really work. You are intended to put the array within the annotation as a constant. Even if you got past this problem, the compiler would reject it. What you need to do is this:

@RunWith(Suite.class)

@Suite.SuiteClasses({MyTest.class, MyOtherTest.class})
public static class TestSuite {
}

注意波浪形括号。

我确定你想要的是能够动态地在套件中构建类列表。

I'm sure what you are trying to get at is to be able to build the list of classes in the suite dynamically.

我提交了请求让他们允许这样做,但与此同时,唯一的方法是将Class类子类化为:

I submitted a request to them to allow that, but in the mean time the only way to do it is to subclass the Suite class like so:

public class DynamicSuite extends Suite {

    public DynamicSuite(Class<?> setupClass) throws InitializationError {
       super(setupClass, DynamicSuiteBuilder.suite());
    }
}


@RunWith(DynamicSuite.class)
public class DynamicSuiteBuilder {
   public static Class[] suite() {
         //Generate class array here.
   }
}

这篇关于带有静态类列表的Junit SuiteClasses的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆