11package com .baeldung .completablefuture ;
22
3- import java .util .concurrent .*;
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertTrue ;
5+
6+ import java .util .concurrent .CancellationException ;
7+ import java .util .concurrent .CompletableFuture ;
8+ import java .util .concurrent .ExecutionException ;
9+ import java .util .concurrent .Executors ;
10+ import java .util .concurrent .Future ;
411import java .util .stream .Collectors ;
512import java .util .stream .Stream ;
613
714import org .junit .Test ;
815
9- import static org .junit .Assert .assertEquals ;
10- import static org .junit .Assert .assertTrue ;
11-
12- public class CompletableFutureTest {
16+ public class CompletableFutureLongRunningUnitTest {
1317
1418 @ Test
1519 public void whenRunningCompletableFutureAsynchronously_thenGetMethodWaitsForResult () throws InterruptedException , ExecutionException {
16-
1720 Future <String > completableFuture = calculateAsync ();
1821
1922 String result = completableFuture .get ();
2023 assertEquals ("Hello" , result );
21-
2224 }
2325
2426 public Future <String > calculateAsync () throws InterruptedException {
@@ -35,12 +37,10 @@ public Future<String> calculateAsync() throws InterruptedException {
3537
3638 @ Test
3739 public void whenRunningCompletableFutureWithResult_thenGetMethodReturnsImmediately () throws InterruptedException , ExecutionException {
38-
3940 Future <String > completableFuture = CompletableFuture .completedFuture ("Hello" );
4041
4142 String result = completableFuture .get ();
4243 assertEquals ("Hello" , result );
43-
4444 }
4545
4646 public Future <String > calculateAsyncWithCancellation () throws InterruptedException {
@@ -57,82 +57,65 @@ public Future<String> calculateAsyncWithCancellation() throws InterruptedExcepti
5757
5858 @ Test (expected = CancellationException .class )
5959 public void whenCancelingTheFuture_thenThrowsCancellationException () throws ExecutionException , InterruptedException {
60-
6160 Future <String > future = calculateAsyncWithCancellation ();
6261 future .get ();
63-
6462 }
6563
6664 @ Test
6765 public void whenCreatingCompletableFutureWithSupplyAsync_thenFutureReturnsValue () throws ExecutionException , InterruptedException {
68-
6966 CompletableFuture <String > future = CompletableFuture .supplyAsync (() -> "Hello" );
7067
7168 assertEquals ("Hello" , future .get ());
72-
7369 }
7470
7571 @ Test
7672 public void whenAddingThenAcceptToFuture_thenFunctionExecutesAfterComputationIsFinished () throws ExecutionException , InterruptedException {
77-
7873 CompletableFuture <String > completableFuture = CompletableFuture .supplyAsync (() -> "Hello" );
7974
8075 CompletableFuture <Void > future = completableFuture .thenAccept (s -> System .out .println ("Computation returned: " + s ));
8176
8277 future .get ();
83-
8478 }
8579
8680 @ Test
8781 public void whenAddingThenRunToFuture_thenFunctionExecutesAfterComputationIsFinished () throws ExecutionException , InterruptedException {
88-
8982 CompletableFuture <String > completableFuture = CompletableFuture .supplyAsync (() -> "Hello" );
9083
9184 CompletableFuture <Void > future = completableFuture .thenRun (() -> System .out .println ("Computation finished." ));
9285
9386 future .get ();
94-
9587 }
9688
9789 @ Test
9890 public void whenAddingThenApplyToFuture_thenFunctionExecutesAfterComputationIsFinished () throws ExecutionException , InterruptedException {
99-
10091 CompletableFuture <String > completableFuture = CompletableFuture .supplyAsync (() -> "Hello" );
10192
10293 CompletableFuture <String > future = completableFuture .thenApply (s -> s + " World" );
10394
10495 assertEquals ("Hello World" , future .get ());
105-
10696 }
10797
10898 @ Test
10999 public void whenUsingThenCompose_thenFuturesExecuteSequentially () throws ExecutionException , InterruptedException {
110-
111100 CompletableFuture <String > completableFuture = CompletableFuture .supplyAsync (() -> "Hello" ).thenCompose (s -> CompletableFuture .supplyAsync (() -> s + " World" ));
112101
113102 assertEquals ("Hello World" , completableFuture .get ());
114-
115103 }
116104
117105 @ Test
118106 public void whenUsingThenCombine_thenWaitForExecutionOfBothFutures () throws ExecutionException , InterruptedException {
119-
120107 CompletableFuture <String > completableFuture = CompletableFuture .supplyAsync (() -> "Hello" ).thenCombine (CompletableFuture .supplyAsync (() -> " World" ), (s1 , s2 ) -> s1 + s2 );
121108
122109 assertEquals ("Hello World" , completableFuture .get ());
123-
124110 }
125111
126112 @ Test
127113 public void whenUsingThenAcceptBoth_thenWaitForExecutionOfBothFutures () throws ExecutionException , InterruptedException {
128-
129114 CompletableFuture .supplyAsync (() -> "Hello" ).thenAcceptBoth (CompletableFuture .supplyAsync (() -> " World" ), (s1 , s2 ) -> System .out .println (s1 + s2 ));
130-
131115 }
132116
133117 @ Test
134118 public void whenFutureCombinedWithAllOfCompletes_thenAllFuturesAreDone () throws ExecutionException , InterruptedException {
135-
136119 CompletableFuture <String > future1 = CompletableFuture .supplyAsync (() -> "Hello" );
137120 CompletableFuture <String > future2 = CompletableFuture .supplyAsync (() -> "Beautiful" );
138121 CompletableFuture <String > future3 = CompletableFuture .supplyAsync (() -> "World" );
@@ -150,12 +133,10 @@ public void whenFutureCombinedWithAllOfCompletes_thenAllFuturesAreDone() throws
150133 String combined = Stream .of (future1 , future2 , future3 ).map (CompletableFuture ::join ).collect (Collectors .joining (" " ));
151134
152135 assertEquals ("Hello Beautiful World" , combined );
153-
154136 }
155137
156138 @ Test
157139 public void whenFutureThrows_thenHandleMethodReceivesException () throws ExecutionException , InterruptedException {
158-
159140 String name = null ;
160141
161142 // ...
@@ -168,12 +149,10 @@ public void whenFutureThrows_thenHandleMethodReceivesException() throws Executio
168149 }).handle ((s , t ) -> s != null ? s : "Hello, Stranger!" );
169150
170151 assertEquals ("Hello, Stranger!" , completableFuture .get ());
171-
172152 }
173153
174154 @ Test (expected = ExecutionException .class )
175155 public void whenCompletingFutureExceptionally_thenGetMethodThrows () throws ExecutionException , InterruptedException {
176-
177156 CompletableFuture <String > completableFuture = new CompletableFuture <>();
178157
179158 // ...
@@ -183,18 +162,15 @@ public void whenCompletingFutureExceptionally_thenGetMethodThrows() throws Execu
183162 // ...
184163
185164 completableFuture .get ();
186-
187165 }
188166
189167 @ Test
190168 public void whenAddingThenApplyAsyncToFuture_thenFunctionExecutesAfterComputationIsFinished () throws ExecutionException , InterruptedException {
191-
192169 CompletableFuture <String > completableFuture = CompletableFuture .supplyAsync (() -> "Hello" );
193170
194171 CompletableFuture <String > future = completableFuture .thenApplyAsync (s -> s + " World" );
195172
196173 assertEquals ("Hello World" , future .get ());
197-
198174 }
199175
200176}
0 commit comments