JUnit: is there a smart way to perform parallel testing?
在我的JUnit测试中,我想执行并行测试。
我的初稿不起作用:
1 2 3 4 5 6 7 | @Test public void parallelTestNotOk() { ExecutorService executor = Executors.newFixedThreadPool(10); Runnable testcaseNotOk = () -> fail("This failure is not raised."); IntStream.range(0, 20).forEach(i -> executor.submit(testcaseNotOk)); executor.shutdown(); } |
尽管每个
我的第二稿起作用了,因为该测试用例按预期失败了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | @Test public void parallelTestOk() throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(10); Callable<AssertionError> testcaseOk = () -> { try { fail("This failure will be raised."); } catch (AssertionError e) { return e; } return null; }; List<Callable<AssertionError>> parallelTests = IntStream .range(0, 20).mapToObj(i -> testcaseOk) .collect(Collectors.toList()); List<AssertionError> allThrownAssertionErrors = executor.invokeAll(parallelTests) .stream().map(future -> { try { return future.get(); } catch (Exception e) { throw new RuntimeException(e); } }).filter(assertionError -> assertionError != null).collect(Collectors.toList()); executor.shutdown(); for (AssertionError e : allThrownAssertionErrors) { throw e; } } |
完成以下操作:
我的
您的问题是您从不检查
这将正确地通过测试:
1 2 3 4 5 6 7 8 9 10 11 12 | @Test public void parallelTestWillFail() throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool(10); Runnable testcaseNotOk = () -> fail("This failure IS raised."); List<Future< ? >> futures = IntStream.range(0, 20) .mapToObj(i -> executor.submit(testcaseNotOk)) .collect(Collectors.toList()); executor.shutdown(); for(Future< ? > f : futures){ f.get(); } } |