... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2007-11-08

Dynamic Test Suites in JUnit 4

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()));
    }

}

Blog Archive