Collect and run all junit tests in parallel with each test class in its own JVM (parallelization by class, not by method)
<p><strong>Problem</strong><br>
I've a bunch of junit tests (many with custom runners such as PowerMockRunner or JUnitParamsRunner) all under some root package <code>tests</code> (they are in various subpackages of <code>tests</code> at various depths).</p>

<p>I'd like to collect all the tests under package <code>tests</code> and run each <strong>test class</strong> in a different JVM, in parallel. Ideally, the parallelization would be configurable, but a default of number_of_cores is totally fine as well. Note that I do not want to run each method in its own JVM, but each class.</p>

<p><strong>Background</strong><br>
I'm using PowerMock combined with JUnitParams via annotations <code>@RunWith(PowerMockRunner.class)</code> and <code>@PowerMockRunnerDelegate(JUnitParamsRunner.class)</code> for many of my tests. I have ~9000 unit tests which complete in an "ok" amount of time but I've an 8-core CPU and the systems is heavily underutilized with the default single-test-at-a-time runner. As I run the tests quite often, the extra time adds up and I really want to run the test classes in parallel.</p>

<p>Note that, unfortunately, in a good number of the tests I need to mock static methods which is part of the reason I'm using PowerMock.</p>

<p><strong>What I've Tried</strong><br>
Having to mock static methods makes it impossible to use something like <code>com.googlecode.junittoolbox.ParallelSuite</code> (which was my initial solution) since it runs everything in the same JVM and the static mocking gets all interleaved and messed up. Or so it seems to me at least based on the errors I get.</p>

<p>I don't know the JUnit stack at all, but after poking around, it appears that another option might be to try to write and inject my own <code>RunnerBuilder</code> -- but I'm not sure if I can even spawn another JVM process from within a <code>RunnerBuilder</code>, unlikely. I think the proper solution would be some kind of harness that lives as a gradle task.</p>

<p>I also JUST discovered some Android Studio (Intellij's) test options but the only available fork option is <code>method</code> which is not what I want. I am currently exploring this solution so perhaps I will figure it out but I thought I'd ask the community in parallel since I haven't had much lock yet.</p>

<p><strong>UPDATE</strong>: Finally was able to get Android Studio (Intellij) to collect all my tests using options Test Kind: <code>All in directory</code> (for some reason the package option did not do recursive searching) and picking fork mode <code>Class</code>. However, this still runs each test class found sequentially and there are no options that I see about parallelization. This is so close to what I want but not quite... :(</p>