|
4 | 4 |
|
5 | 5 | import java.util.concurrent.CompletableFuture; |
6 | 6 |
|
7 | | -import static org.assertj.core.api.Java6Assertions.assertThat; |
8 | | - |
9 | 7 | public class CompletableFutureExceptionHandlingTest { |
10 | 8 | @Test |
11 | | - public void handle() { |
| 9 | + public void handleWithException() { |
12 | 10 | CompletableFuture completableFuture = CompletableFuture |
13 | 11 | .supplyAsync(() -> Integer.parseInt("s")) |
14 | 12 | .handleAsync((result, e) -> { |
15 | 13 | if (e != null) { |
16 | 14 | System.out.println(e.getMessage()); |
17 | 15 | return "Error!"; |
18 | 16 | } else { |
19 | | - return result; |
| 17 | + return result + 1; |
20 | 18 | } |
21 | 19 | }) |
22 | 20 | .thenAcceptAsync(System.out::println); |
| 21 | + } |
23 | 22 |
|
24 | | - assertThat(completableFuture.join()) |
25 | | - .isNull(); |
| 23 | + @Test |
| 24 | + public void handleWithoutExceptions() { |
| 25 | + CompletableFuture completableFuture = CompletableFuture |
| 26 | + .supplyAsync(() -> 1) |
| 27 | + .handleAsync((result, e) -> { |
| 28 | + if (e != null) { |
| 29 | + System.out.println(e.getMessage()); |
| 30 | + return "Error!"; |
| 31 | + } else { |
| 32 | + return result + 1; |
| 33 | + } |
| 34 | + }) |
| 35 | + .thenAcceptAsync(System.out::println); |
26 | 36 | } |
27 | 37 |
|
28 | 38 | @Test |
29 | | - public void exceptionally() { |
| 39 | + public void exceptionallyWithException() { |
30 | 40 | CompletableFuture completableFuture = CompletableFuture |
31 | 41 | .supplyAsync(() -> Integer.parseInt("s")) |
32 | 42 | .exceptionallyAsync((e) -> { |
33 | 43 | System.out.println(e.getMessage()); |
34 | | - return null; |
| 44 | + return 0; |
35 | 45 | }) |
36 | 46 | .thenAcceptAsync(System.out::println); |
| 47 | + } |
37 | 48 |
|
38 | | - assertThat(completableFuture.join()) |
39 | | - .isNull(); |
| 49 | + @Test |
| 50 | + public void exceptionallyWithoutExceptions() { |
| 51 | + CompletableFuture completableFuture = CompletableFuture |
| 52 | + .supplyAsync(() -> 1) |
| 53 | + .exceptionallyAsync((e) -> { |
| 54 | + System.out.println(e.getMessage()); |
| 55 | + return 0; |
| 56 | + }) |
| 57 | + .thenAcceptAsync(System.out::println); |
40 | 58 | } |
41 | 59 |
|
42 | 60 | @Test |
43 | | - public void whenComplete() { |
| 61 | + public void whenCompleteWithException() { |
44 | 62 | CompletableFuture completableFuture = CompletableFuture |
45 | 63 | .supplyAsync(() -> Integer.parseInt("s")) |
46 | 64 | .whenCompleteAsync((result, e) -> { |
47 | 65 | if (e != null) System.out.println(e.getMessage()); |
48 | 66 | }) |
49 | 67 | .thenAcceptAsync(System.out::println); |
| 68 | + } |
50 | 69 |
|
51 | | - assertThat(completableFuture.join()) |
52 | | - .isNull(); |
| 70 | + @Test |
| 71 | + public void whenCompleteWithoutExceptions() { |
| 72 | + CompletableFuture completableFuture = CompletableFuture |
| 73 | + .supplyAsync(() -> 1) |
| 74 | + .whenCompleteAsync((result, e) -> { |
| 75 | + if (e != null) System.out.println(e.getMessage()); |
| 76 | + }) |
| 77 | + .thenAcceptAsync(System.out::println); |
53 | 78 | } |
54 | 79 | } |
0 commit comments