Skip to content

Commit 2ece010

Browse files
vunamtientienvn
andauthored
BAEL-8021-Guide-to-CompletableFuture-join-vs-get (#16800)
* BAEL-8021-Guide-to-CompletableFuture-join-vs-get * BAEL-8021-Guide-to-CompletableFuture-join-vs-get * BAEL-8021-Guide-to-CompletableFuture-join-vs-get * BAEL-8021-Guide-to-CompletableFuture-join-vs-get --------- Co-authored-by: tienvn <[email protected]>
1 parent aa52a64 commit 2ece010

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.joinvsget;
2+
3+
import org.junit.jupiter.api.Test;
4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.concurrent.CompletionException;
6+
import java.util.concurrent.ExecutionException;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
public class CompletableFutureUnitTest {
10+
11+
@Test
12+
public void givenJoinMethod_whenThrow_thenGetUncheckedException() {
13+
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Test join");
14+
15+
assertEquals("Test join", future.join());
16+
17+
CompletableFuture<String> exceptionFuture = CompletableFuture.failedFuture(new RuntimeException("Test join exception"));
18+
19+
assertThrows(CompletionException.class, exceptionFuture::join);
20+
}
21+
22+
@Test
23+
public void givenGetMethod_whenThrow_thenGetCheckedException() {
24+
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Test get");
25+
26+
try {
27+
assertEquals("Test get", future.get());
28+
} catch (InterruptedException | ExecutionException e) {
29+
fail("Exception should not be thrown");
30+
}
31+
32+
CompletableFuture<String> exceptionFuture = CompletableFuture.failedFuture(new RuntimeException("Test get exception"));
33+
34+
assertThrows(ExecutionException.class, exceptionFuture::get);
35+
}
36+
}

0 commit comments

Comments
 (0)