JUnit: is there a smart way to perform parallel testing?
<p>In my JUnit tests I want to perform parallel tests.</p>

<p>My first draft does not work:</p>

<pre><code>@Test
public void parallelTestNotOk() {
    ExecutorService executor = Executors.newFixedThreadPool(10);
    Runnable testcaseNotOk = () -&gt; fail("This failure is not raised.");
    IntStream.range(0, 20).forEach(i -&gt; executor.submit(testcaseNotOk));
    executor.shutdown();
}
</code></pre>

<p>Although each <code>testcaseNotOk</code> fails, this testcase succeeds. Why? Because <code>fail</code> is not called in the main thread, but in a parallel one?</p>

<p>My second draft works, because this testcase fails as expected:</p>

<pre><code>@Test
public void parallelTestOk() throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(10);
    Callable&lt;AssertionError&gt; testcaseOk = () -&gt; {
        try {
            fail("This failure will be raised.");
        } catch (AssertionError e) {
            return e;
        }
        return null;
    };
    List&lt;Callable&lt;AssertionError&gt;&gt; parallelTests = IntStream
            .range(0, 20).mapToObj(i -&gt; testcaseOk)
            .collect(Collectors.toList());
    List&lt;AssertionError&gt; allThrownAssertionErrors = executor.invokeAll(parallelTests)
      .stream().map(future -&gt; {
        try {
            return future.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }).filter(assertionError -&gt; assertionError != null).collect(Collectors.toList());
    executor.shutdown();
    for (AssertionError e : allThrownAssertionErrors) {
        throw e;
    }
}
</code></pre>

<p>The following is done:</p>

<ol>
<li>In <code>testcaseOk</code> the code to be tested is embedded into a try/catch block and every <code>AssertionError</code> rethrown.</li>
<li><code>parallelTests</code> contains 20 times <code>testcaseOk</code>.</li>
<li>The <code>ExecutorService</code> performs all <code>parallelTests</code>.</li>
<li>If an <code>AssertionError</code> is thrown in <code>testcaseOk</code>, it will be collected into <code>allThrownAssertionErrors</code>.</li>
<li>If <code>allThrownAssertionErrors</code> contains any <code>AssertionError</code>, it will be thrown and the testcase <code>parallelTestOk()</code> will fail. Otherwise it will be ok.</li>
</ol>

<p>My <code>parallelTestOk()</code> seems to be quite complicated. Is there an easier and smarter way (without using TestNG)?</p>