Programmatically run JUnit in Parallel

To speed up running tests, parallel execution is commonly used strategy. When programmatically runnning JUnit tests via JunitCore, the code have to incorporate with the parallel execution logic as well.

In junit 4.12, there is a class called org.junit.experimental.ParallelComputer to parallel run tests.

However, this class is not much usable. First, maybe this is a trivial reason though, it is an experimental feature as the package name indicates. The biggest problem is that ParallelComputer does not give any configuration. It just makes tests run in parallel either by classes or methods.

Alternatively, I found a parallel run implementation in maven surefire.

  • org.apache.maven.surefire.junitcore.pc.ParallelComputer
  • org.apache.maven.surefire.junitcore.pc.ParallelComputerBuilder

They can be used in conjunction with JUnitCore.

Here is how:

Add a dependency to one of the surefire libraries which includes parallel run implementation.


In code, prepare properties, build ParallelComputer, then pass it to junitCore.

// run junit with concurrency configured
Properties properties = new Properties();
properties.put(JUnitCoreParameters.PARALLEL_KEY, "classes");
properties.put(JUnitCoreParameters.THREADCOUNT_KEY, String.valueOf(numOfThread));
properties.put(JUnitCoreParameters.PERCORETHREADCOUNT_KEY, "false");  // default is true

JUnitCoreParameters parameters = new JUnitCoreParameters(properties);
ParallelComputerBuilder builder = new ParallelComputerBuilder(
  new DefaultConsoleReporter(System.out), parameters);
ParallelComputer computer = builder.buildComputer();

// run tests in parallel
Result result =, MyTestSuite.class);

To figure out parameters, you may need to see actual implementation and surefire API web page.
The ParallelComputer in surefire is widely used via mvn test, and is powerfull enough to make unittests run in parallel.

Written on September 29, 2015