Actually I think the best solution on this case is to write your own org.junit.Runner
. It is not so complicated as it seems. A simple sample would be:
The Runner:
package foo.bar.test;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.JUnit4;
import org.junit.runners.model.InitializationError;
public class MyRunner extends Runner {
private final Runner runner;
public MyRunner(final Class<?> klass) throws InitializationError {
super();
this.runner = new JUnit4(klass);
}
@Override
public Description getDescription() {
return runner.getDescription();
}
@Override
public void run(final RunNotifier notifier) {
for (Description description : runner.getDescription().getChildren()) {
notifier.fireTestStarted(description);
try {
// here it is possible to get annotation:
// description.getAnnotation(annotationType)
if (MyConfiguration.shallExecute(description.getClassName(), description.getMethodName())) {
runner.run(notifier);
}
} catch (Exception e) {
notifier.fireTestFailure(new Failure(description, e));
}
}
}
}
The test case:
package foo.bar.test;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(MyRunner.class)
public class TestCase {
@Test
public void myTest() {
System.out.println("executed");
}
}
And configuration class:
package foo.bar.test;
public class MyConfiguration {
public static boolean shallExecute(final String className, final String methodName) {
// your configuration logic
System.out.println(className + "." + methodName);
return false;
}
}
Here the cool thing is that you could implement your own annotation, for example: @TestKey("testWithDataBase")
, see comments on the example source above. And your configuration object could define if the test should run or not, so you can group tests, what is quite useful when you have a lot of tests that needs to be grouped.