Skip to content

Commit

Permalink
test/refactor: use assertThrows instead of try-fail-catch test pa…
Browse files Browse the repository at this point in the history
…ttern 🧪
  • Loading branch information
oldratlee committed Jun 7, 2024
1 parent e137882 commit e6fb62a
Showing 1 changed file with 57 additions and 123 deletions.
180 changes: 57 additions & 123 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ void test_completedStage() throws Exception {
void test_failedFuture() throws Exception {
Cffu<Integer> 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);
Expand All @@ -75,13 +70,8 @@ void test_failedStage() throws Exception {
CompletionStage<Integer> sa = stage.thenApply(identity());
CompletionStage<Integer> 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -751,18 +692,11 @@ void test_forbidObtrudeMethods_property() {
CffuFactory fac2 = CffuFactory.builder(executorService).forbidObtrudeMethods(true).build();

Cffu<Object> 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
Expand Down

0 comments on commit e6fb62a

Please sign in to comment.