diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java index 3dfa535c..9f4c46c3 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -58,12 +58,7 @@ void test_completedStage() throws Exception { void test_failedFuture() throws Exception { Cffu cf = cffuFactory.failedFuture(rte); - try { - cf.join(); - fail(); - } catch (CompletionException expected) { - assertSame(rte, expected.getCause()); - } + assertSame(rte, assertThrows(CompletionException.class, cf::join).getCause()); assertEquals(n, cf.exceptionally(throwable -> n).get()); shouldNotBeMinimalStage(cf); @@ -75,13 +70,8 @@ void test_failedStage() throws Exception { CompletionStage sa = stage.thenApply(identity()); CompletionStage se = stage.exceptionally(throwable -> n); - try { - failedFuture(rte).toCompletableFuture().join(); - fail(); - } catch (CompletionException expected) { - assertSame(rte, expected.getCause()); - - } + assertSame(rte, assertThrows(CompletionException.class, () -> + failedFuture(rte).toCompletableFuture().join()).getCause()); assertEquals(n, se.toCompletableFuture().get()); // CAUTION: Last check minimal stage, may rewrite the CF by obtrude* methods @@ -127,12 +117,8 @@ void test_toCffu() throws Exception { assertSame(anotherExecutorService, cffu.defaultExecutor()); assertSame(fac, cffu.cffuFactory()); - try { - cffu.obtrudeValue(44); - fail(); - } catch (UnsupportedOperationException expected) { - assertEquals("obtrude methods is forbidden by cffu", expected.getMessage(), expected.getMessage()); - } + assertEquals("obtrude methods is forbidden by cffu", + assertThrows(UnsupportedOperationException.class, () -> cffu.obtrudeValue(44)).getMessage()); } @Test @@ -195,12 +181,8 @@ void test_anyOf_CompletableFuture() throws Exception { cffuFactory.anyOfSuccess(completedFuture(n), completedFuture(anotherN)).get(); assertEquals(anotherN, cffuFactory.anyOfSuccess(completedFuture(anotherN)).get()); - try { - cffuFactory.anyOfSuccess().get(); - fail(); - } catch (ExecutionException expected) { - assertSame(NoCfsProvidedException.class, expected.getCause().getClass()); - } + assertInstanceOf(NoCfsProvidedException.class, assertThrows(ExecutionException.class, () -> + cffuFactory.anyOfSuccess().get()).getCause()); cffuFactory.anyOfSuccess( cffuFactory.completedFuture(n), @@ -269,29 +251,19 @@ void test_allResultsOf() throws Exception { @Test void test_allResultsOf_exceptionally() throws Exception { - try { - cffuFactory.allResultsOf( - cffuFactory.completedFuture(n), - cffuFactory.failedFuture(rte), - cffuFactory.completedFuture(s) - ).get(); - - fail(); - } catch (ExecutionException expected) { - assertSame(rte, expected.getCause()); - } - - try { - cffuFactory.allResultsOfFastFail( - cffuFactory.completedFuture(n), - cffuFactory.failedFuture(rte), - cffuFactory.completedFuture(s) - ).get(); - - fail(); - } catch (ExecutionException expected) { - assertSame(rte, expected.getCause()); - } + assertSame(rte, assertThrows(ExecutionException.class, () -> + cffuFactory.allResultsOf( + cffuFactory.completedFuture(n), + cffuFactory.failedFuture(rte), + cffuFactory.completedFuture(s) + ).get()).getCause()); + + assertSame(rte, assertThrows(ExecutionException.class, () -> + cffuFactory.allResultsOfFastFail( + cffuFactory.completedFuture(n), + cffuFactory.failedFuture(rte), + cffuFactory.completedFuture(s) + ).get()).getCause()); } @Test @@ -360,13 +332,9 @@ void test_anyOf_exceptionally() throws Exception { // first exceptionally completed anyOf cf win, // even later cfs normally completed! - try { - cffuFactory.anyOf(createIncompleteFuture(), failedFuture(rte), createIncompleteFuture()).get(); - - fail(); - } catch (ExecutionException expected) { - assertSame(rte, expected.getCause()); - } + assertSame(rte, assertThrows(ExecutionException.class, () -> + cffuFactory.anyOf(createIncompleteFuture(), failedFuture(rte), createIncompleteFuture()).get() + ).getCause()); // first normally completed anyOf cf win, // even later cfs exceptionally completed! @@ -519,35 +487,21 @@ void test_allTupleOf() throws Exception { @Test void test_allTupleOf_exceptionally() throws Exception { - try { - cffuFactory.allTupleOf(completedFuture(n), failedFuture(rte)).get(); + assertSame(rte, assertThrows(ExecutionException.class, () -> + cffuFactory.allTupleOf(completedFuture(n), failedFuture(rte)).get() + ).getCause()); - fail(); - } catch (ExecutionException expected) { - assertSame(rte, expected.getCause()); - } - - try { - cffuFactory.allTupleOf(completedFuture(n), failedFuture(rte), completedFuture(s)).get(); - - fail(); - } catch (ExecutionException expected) { - assertSame(rte, expected.getCause()); - } + assertSame(rte, assertThrows(ExecutionException.class, () -> + cffuFactory.allTupleOf(completedFuture(n), failedFuture(rte), completedFuture(s)).get() + ).getCause()); - try { - cffuFactory.allTupleOf( - completedFuture(n), - completedFuture(d), - failedFuture(rte), - completedFuture(s), - completedFuture(anotherN) - ).get(); - - fail(); - } catch (ExecutionException expected) { - assertSame(rte, expected.getCause()); - } + assertSame(rte, assertThrows(ExecutionException.class, () -> cffuFactory.allTupleOf( + completedFuture(n), + completedFuture(d), + failedFuture(rte), + completedFuture(s), + completedFuture(anotherN) + ).get()).getCause()); } @Test @@ -609,35 +563,22 @@ void test_allTupleOfFastFail() throws Exception { @Test void test_allTupleOfFastFail_exceptionally() throws Exception { - try { - cffuFactory.allTupleOfFastFail(completedFuture(n), failedFuture(rte)).get(); - - fail(); - } catch (ExecutionException expected) { - assertSame(rte, expected.getCause()); - } - - try { - cffuFactory.allTupleOfFastFail(completedFuture(n), failedFuture(rte), completedFuture(s)).get(); - - fail(); - } catch (ExecutionException expected) { - assertSame(rte, expected.getCause()); - } - - try { - cffuFactory.allTupleOfFastFail( - completedFuture(n), - completedFuture(d), - failedFuture(rte), - completedFuture(s), - completedFuture(anotherN) - ).get(); - - fail(); - } catch (ExecutionException expected) { - assertSame(rte, expected.getCause()); - } + assertSame(rte, assertThrows(ExecutionException.class, () -> + cffuFactory.allTupleOfFastFail(completedFuture(n), failedFuture(rte)).get() + ).getCause()); + + assertSame(rte, assertThrows(ExecutionException.class, () -> + cffuFactory.allTupleOfFastFail(completedFuture(n), failedFuture(rte), completedFuture(s)).get() + ).getCause()); + + assertSame(rte, assertThrows(ExecutionException.class, () -> cffuFactory.allTupleOfFastFail( + completedFuture(n), + completedFuture(d), + failedFuture(rte), + completedFuture(s), + completedFuture(anotherN) + ).get() + ).getCause()); } @Test @@ -751,18 +692,11 @@ void test_forbidObtrudeMethods_property() { CffuFactory fac2 = CffuFactory.builder(executorService).forbidObtrudeMethods(true).build(); Cffu cf = fac2.newIncompleteCffu(); - try { - cf.obtrudeValue(42); - fail(); - } catch (UnsupportedOperationException expected) { - assertEquals("obtrude methods is forbidden by cffu", expected.getMessage()); - } - try { - cf.obtrudeException(rte); - fail(); - } catch (UnsupportedOperationException expected) { - assertEquals("obtrude methods is forbidden by cffu", expected.getMessage(), expected.getMessage()); - } + + assertEquals("obtrude methods is forbidden by cffu", assertThrows(UnsupportedOperationException.class, + () -> cf.obtrudeValue(42)).getMessage()); + assertEquals("obtrude methods is forbidden by cffu", assertThrows(UnsupportedOperationException.class, + () -> cf.obtrudeException(rte)).getMessage()); } @Test