I'm having a few Junit test classes. I want to run them in 2 threads, so included maxParallelForks = 2
. I want to make sure that tests of same class run in same thread sequentially. How to achieve this? (I use SpringRunner.)
Asked
Active
Viewed 706 times
0

Santhosh Kumar
- 314
- 2
- 9
2 Answers
1
I was using @RunWith(Suite.class)
to run multiple test classes. So I created a new Runner class and this solved my problem.
public class ParallelExecutor extends Suite {
public ParallelExecutor(Class<?> klass, RunnerBuilder builder) throws InitializationError, IOException, InterruptedException {
super(klass, builder);
setScheduler(new RunnerScheduler() {
private final ExecutorService service = Executors.newFixedThreadPool(10);
public void schedule(Runnable childStatement) {
service.submit(childStatement);
}
public void finished() {
try {
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
});
}
}

Santhosh Kumar
- 314
- 2
- 9
0
As far as I can see from a quick look at the Gradle sources, this should exactly be the option you want. maxParallelForks
make test classes be executed in parallel, not single test methods.

Vampire
- 35,631
- 4
- 76
- 102