java 带有静态类列表的 Junit SuiteClasses

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1070202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:02:04  来源:igfitidea点击:

Junit SuiteClasses with a static list of classes

javajunit4

提问by Yishai

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<?>[]

SuiteClasses 可以很好地处理诸如 之类的类列表{Test1.class,Test2.class},但是当我尝试生成类的静态列表时,它显示不兼容的类型:需要java.lang.Class<?>但已找到java.lang.Class<?>[]

What am I missing?

我错过了什么?

@RunWith(Suite.class)

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

    public static Class<?> [] classes;

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

回答by Yishai

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 {
}

Note the squiggly brackets.

请注意波浪形括号。

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

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

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

我向他们提交了一个请求以允许这样做,但与此同时,唯一的方法是像这样子类化 Suite 类:

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.
   }
}

回答by ashu

@SuiteClassesis a class annotation defined in JUnit 4.4 in org.junit.runners.Suite.SuiteClasses. It allows you to define a suite class as described in the previous question.

@SuiteClasses是 org.junit.runners.Suite.SuiteClasses 中 JUnit 4.4 中定义的类注释。它允许您定义一个套件类,如上一个问题中所述。

By the way, the API document of JUnit 4.4 has a major typo for the org.junit.runners.Suite class (Suite.html).

顺便说一下,JUnit 4.4 的 API 文档对于 org.junit.runners.Suite 类 (Suite.html) 有一个主要的拼写错误。

Using Suite as a runner allows you to manually build a suite containing tests from many classes. It is the JUnit 4 equivalent of the JUnit 3.8.x static Test suite()method. To use it, annotate a class with @RunWith(Suite.class)and @SuiteClasses(TestClass1.class, ...). When you run this class, it will run all the tests in all the suite classes.

使用 Suite 作为运行器允许您手动构建包含来自许多类的测试的套件。它是 JUnit 4 等效的 JUnit 3.8.x 静态测试suite()方法。要使用它,请用@RunWith(Suite.class)和注释一个类@SuiteClasses(TestClass1.class, ...)。当您运行这个类时,它将运行所有套件类中的所有测试。

@SuiteClasses(TestClass1.class, ...)should be changed to @Suite.SuiteClasses({TestClass1.class, ...}).

@SuiteClasses(TestClass1.class, ...)应该改为@Suite.SuiteClasses({TestClass1.class, ...}).

Someone provided wrong information on build test suite in JUnit 4.4. Do not follow this:

有人在 JUnit 4.4 中提供了有关构建测试套件的错误信息。不要遵循这个:

JUnit provides tools to define the suite to be run and to display its results. To run tests and see the results on the console, run:

JUnit 提供工具来定义要运行的套件并显示其结果。要运行测试并在控制台上查看结果,请运行:

org.junit.runner.TextListener.run(TestClass1.class, ...);