You can accomplish this with the JUnit ParallelComputer (note that it is still considered experimental). This is a fairly simple implementation that is supported by the java.util.concurrent.ExecutorService API.
If you are interested in how this works, check out the source .
Basically you call JUnitCore.runClasses(Computer, Classes ...) and pass in a ParallelComputer object for the first argument.
Usage example:
import org.junit.Test; import org.junit.experimental.ParallelComputer; import org.junit.runner.JUnitCore; public class ParallelComputerExample { @Test public void runAllTests() { Class<?>[] classes = { ParallelTest1.class, ParallelTest2.class };
The above code will work after 3 seconds, since all methods and classes are executed in parallel.
This will work after 6 seconds (because all classes are parallel). JUnitCore.runClasses(new ParallelComputer(true, false), classes);
This will also work after 6 seconds (because all methods are parallel). JUnitCore.runClasses(new ParallelComputer(false, true), classes);
Andy guibert
source share