Here is how you create a dynamic test suite using the JUnit 4 (instead of the old JUnit 3 suite mechanism):
@RunWith(MyRunner.class)
public class MyRunner extends Runner {
private static final Description TEST1 = Description.createSuiteDescription("test1");
private static final Description TEST2 = Description.createSuiteDescription("test2");
public MyRunner(Class<?> testClass) {
// required by JUnit
}
@Override
public Description getDescription() {
Description description = Description.createSuiteDescription(MyRunner.class);
description.addChild(TEST1);
description.addChild(TEST2);
return description;
}
@Override
public void run(RunNotifier notifier) {
notifier.fireTestFinished(TEST1);
notifier.fireTestFailure(new Failure(TEST2, new RuntimeException()));
}
}