Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - Junit SuiteClasses with a static list of classes

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

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...