I have created two Runners both of whom are inherited from BlockJUnit4ClassRunner. One is to filter some tests based on conditions. The other is to run test multiple methods concurrently. I want to apply both these runner so that the tests are run based on conditions and concurrently.
The problem is we cannot annotate multiple @RunWith to the same tests, according to Multiple RunWith Statements in jUnit.
my question is since both these two runners are inherited from the same base class BlockJUnit4ClassRunner, can I combine these two runners to be one so that RunWith this runner, I can run test methods based on conditions and using concurrent way? And How to do that?
Here are the two runners I created:
1) This runner runs test methods based on some conditions defined in FunctionalTestFilter:
public class FilterRunner extends BlockJUnit4ClassRunner {
public FilterRunner(Class<?> clazz) throws InitializationError {
super(clazz);
Filter f = new FunctionalTestFilter();
try {
f.apply(this);
} catch (NoTestsRemainException ex) {
System.out.println("Warning! No method run in " + clazz.getName());
}
}
2) This runner runs test methods concurrently using multi-threading:
public static class ConcurrentJunitMethodRunner extends BlockJUnit4ClassRunner {
public ConcurrentJunitMethodRunner(final Class<?> testclass) throws InitializationError {
super(testclass);
setScheduler(new RunnerScheduler() {
ExecutorService executorService = Executors.newFixedThreadPool(5);
CompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
Queue<Future<Void>> tasks = new LinkedList<Future<Void>>();
@Override
public void schedule(Runnable childStatement) {
tasks.offer(completionService.submit(childStatement, null));
}
@Override
public void finished() {
try {
while (!tasks.isEmpty())
tasks.remove(completionService.take());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
while (!tasks.isEmpty())
tasks.poll().cancel(true);
executorService.shutdownNow();
}
}
});
}
}