Skip to content

Commit 5195a43

Browse files
forketyforkpivovarit
authored andcommitted
Added test for the Guide to CompletableFuture (eugenp#611)
1 parent 2f331ce commit 5195a43

1 file changed

Lines changed: 196 additions & 0 deletions

File tree

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package com.baeldung.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.Future;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
10+
import org.junit.Test;
11+
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertTrue;
14+
15+
public class CompletableFutureTest {
16+
17+
@Test
18+
public void whenRunningCompletableFutureAsyncronously_thenGetMethodWaitsForResult() throws InterruptedException, ExecutionException {
19+
20+
Future<String> completableFuture = calculateAsync();
21+
22+
String result = completableFuture.get();
23+
assertEquals("Hello", result);
24+
25+
}
26+
27+
public Future<String> calculateAsync() throws InterruptedException {
28+
CompletableFuture<String> completableFuture = new CompletableFuture<>();
29+
30+
Executors.newCachedThreadPool().submit(() -> {
31+
Thread.sleep(500);
32+
completableFuture.complete("Hello");
33+
return null;
34+
});
35+
36+
return completableFuture;
37+
}
38+
39+
@Test
40+
public void whenRunningCompletableFutureWithResult_thenGetMethodReturnsImmediately() throws InterruptedException, ExecutionException {
41+
42+
Future<String> completableFuture = CompletableFuture.completedFuture("Hello");
43+
44+
String result = completableFuture.get();
45+
assertEquals("Hello", result);
46+
47+
}
48+
49+
@Test
50+
public void whenCreatingCompletableFutureWithSupplyAsync_thenFutureReturnsValue() throws ExecutionException, InterruptedException {
51+
52+
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
53+
54+
assertEquals("Hello", future.get());
55+
56+
}
57+
58+
@Test
59+
public void whenAddingThenAcceptToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
60+
61+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
62+
63+
CompletableFuture<Void> future = completableFuture.thenAccept(s -> System.out.println("Computation returned: " + s));
64+
65+
future.get();
66+
67+
}
68+
69+
@Test
70+
public void whenAddingThenRunToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
71+
72+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
73+
74+
CompletableFuture<Void> future = completableFuture.thenRun(() -> System.out.println("Computation finished."));
75+
76+
future.get();
77+
78+
}
79+
80+
@Test
81+
public void whenAddingThenApplyToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
82+
83+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
84+
85+
CompletableFuture<String> future = completableFuture.thenApply(s -> s + " World");
86+
87+
assertEquals("Hello World", future.get());
88+
89+
}
90+
91+
@Test
92+
public void whenUsingThenCompose_thenFuturesExecuteSequentially() throws ExecutionException, InterruptedException {
93+
94+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello")
95+
.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World"));
96+
97+
assertEquals("Hello World", completableFuture.get());
98+
99+
}
100+
101+
@Test
102+
public void whenUsingThenCombine_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException {
103+
104+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello")
105+
.thenCombine(CompletableFuture.supplyAsync(() -> " World"),
106+
(s1, s2) -> s1 + s2);
107+
108+
assertEquals("Hello World", completableFuture.get());
109+
110+
}
111+
112+
@Test
113+
public void whenUsingThenAcceptBoth_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException {
114+
115+
CompletableFuture.supplyAsync(() -> "Hello")
116+
.thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"),
117+
(s1, s2) -> System.out.println(s1 + s2));
118+
119+
}
120+
121+
@Test
122+
public void whenFutureCombinedWithAllOfCompletes_thenAllFuturesAreDone() throws ExecutionException, InterruptedException {
123+
124+
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
125+
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "Beautiful");
126+
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> "World");
127+
128+
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3);
129+
130+
// ...
131+
132+
combinedFuture.get();
133+
134+
assertTrue(future1.isDone());
135+
assertTrue(future2.isDone());
136+
assertTrue(future3.isDone());
137+
138+
String combined = Stream.of(future1, future2, future3)
139+
.map(CompletableFuture::join)
140+
.collect(Collectors.joining(" "));
141+
142+
assertEquals("Hello Beautiful World", combined);
143+
144+
}
145+
146+
@Test
147+
public void whenFutureThrows_thenHandleMethodRecievesException() throws ExecutionException, InterruptedException {
148+
149+
String name = null;
150+
151+
// ...
152+
153+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
154+
if (name == null) {
155+
throw new RuntimeException("Computation error!");
156+
}
157+
return "Hello, " + name;
158+
}).handle((s, t) -> {
159+
if (s != null) {
160+
return s;
161+
} else {
162+
return "Hello, Stranger!";
163+
}
164+
});
165+
166+
assertEquals("Hello, Stranger!", completableFuture.get());
167+
168+
}
169+
170+
@Test(expected = ExecutionException.class)
171+
public void whenCompletingFutureExceptionally_thenGetMethodThrows() throws ExecutionException, InterruptedException {
172+
173+
CompletableFuture<String> completableFuture = new CompletableFuture<>();
174+
175+
// ...
176+
177+
completableFuture.completeExceptionally(new RuntimeException("Calculation failed!"));
178+
179+
// ...
180+
181+
completableFuture.get();
182+
183+
}
184+
185+
@Test
186+
public void whenAddingThenApplyAsyncToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
187+
188+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
189+
190+
CompletableFuture<String> future = completableFuture.thenApplyAsync(s -> s + " World");
191+
192+
assertEquals("Hello World", future.get());
193+
194+
}
195+
196+
}

0 commit comments

Comments
 (0)