44
55import java .util .concurrent .CompletableFuture ;
66import java .util .concurrent .ExecutionException ;
7+ import java .util .concurrent .Executors ;
78
89import org .junit .Test ;
910
1516
1617public class FutureTest {
1718
19+ @ Test
20+ public void whenChangeExecutorService_thenCorrect () {
21+ String initialValue = "Welcome to " ;
22+ Future <String > resultFuture = Future .of (
23+ Executors .newSingleThreadExecutor (),
24+ () -> Util .appendData (initialValue ));
25+ String result = resultFuture .get ();
26+
27+ assertThat (result ).isEqualTo ("Welcome to Baeldung!" );
28+ }
29+
1830 @ Test
1931 public void whenAppendData_thenCorrect1 () {
2032 String initialValue = "Welcome to " ;
@@ -40,8 +52,8 @@ public void whenAppendData_thenCorrect2() {
4052 public void whenAppendData_thenSuccess () {
4153 String initialValue = "Welcome to " ;
4254 Future <String > resultFuture = Future .of (() -> Util .appendData (initialValue ))
43- .onSuccess (finalResult -> System .out .println ("Successfully Completed - Result: " + finalResult ))
44- .onFailure (finalResult -> System .out .println ("Failed - Result: " + finalResult ));
55+ .onSuccess (finalResult -> System .out .println ("Successfully Completed - Result: " + finalResult ))
56+ .onFailure (finalResult -> System .out .println ("Failed - Result: " + finalResult ));
4557 String result = resultFuture .get ();
4658
4759 assertThat (result ).isEqualTo ("Welcome to Baeldung!" );
@@ -51,8 +63,8 @@ public void whenAppendData_thenSuccess() {
5163 public void whenChainingCallbacks_thenCorrect () {
5264 String initialValue = "Welcome to " ;
5365 Future <String > resultFuture = Future .of (() -> Util .appendData (initialValue ))
54- .andThen (finalResult -> System .out .println ("Completed - 1: " + finalResult ))
55- .andThen (finalResult -> System .out .println ("Completed - 2: " + finalResult ));
66+ .andThen (finalResult -> System .out .println ("Completed - 1: " + finalResult ))
67+ .andThen (finalResult -> System .out .println ("Completed - 2: " + finalResult ));
5668 String result = resultFuture .get ();
5769
5870 assertThat (result ).isEqualTo ("Welcome to Baeldung!" );
@@ -109,14 +121,14 @@ public void whenAppendData_thenFutureNotEmpty() {
109121 @ Test
110122 public void whenCallZip_thenCorrect () {
111123 Future <Tuple2 <String , Integer >> future = Future .of (() -> "John" )
112- .zip (Future .of (() -> new Integer (5 )));
124+ .zip (Future .of (() -> new Integer (5 )));
113125 future .await ();
114126
115127 assertThat (future .get ()).isEqualTo (Tuple .of ("John" , new Integer (5 )));
116128 }
117129
118130 @ Test
119- public void whenAppendData_thenFutureNotEmptyd () throws InterruptedException , ExecutionException {
131+ public void whenConvertToCompletableFuture_thenCorrect () throws InterruptedException , ExecutionException {
120132 String initialValue = "Welcome to " ;
121133 Future <String > resultFuture = Future .of (() -> Util .appendData (initialValue ));
122134 CompletableFuture <String > convertedFuture = resultFuture .toCompletableFuture ();
@@ -127,9 +139,39 @@ public void whenAppendData_thenFutureNotEmptyd() throws InterruptedException, Ex
127139 @ Test
128140 public void whenCallMap_thenCorrect () {
129141 Future <String > futureResult = Future .of (() -> new StringBuilder ("from Baeldung" ))
130- .map (a -> "Hello " + a );
142+ .map (a -> "Hello " + a );
131143 futureResult .await ();
132144
133145 assertThat (futureResult .get ()).isEqualTo ("Hello from Baeldung" );
134146 }
147+
148+ @ Test
149+ public void whenFutureFails_thenGetErrorMessage () {
150+ Future <String > resultFuture = Future .of (() -> Util .getSubstringMinusOne ("Hello" ));
151+ Future <String > errorMessageFuture = resultFuture .recover (Throwable ::getMessage );
152+ String errorMessage = errorMessageFuture .get ();
153+
154+ assertThat (errorMessage ).isEqualTo ("String index out of range: -1" );
155+ }
156+
157+ @ Test
158+ public void whenFutureFails_thenGetAnotherFuture () {
159+ Future <String > resultFuture = Future .of (() -> Util .getSubstringMinusOne ("Hello" ));
160+ Future <String > errorMessageFuture = resultFuture .recoverWith (a -> Future .of (a ::getMessage ));
161+ String errorMessage = errorMessageFuture .get ();
162+
163+ assertThat (errorMessage ).isEqualTo ("String index out of range: -1" );
164+ }
165+
166+ @ Test
167+ public void whenBothFuturesFail_thenGetErrorMessage () {
168+ Future <String > future1 = Future .of (() -> Util .getSubstringMinusOne ("Hello" ));
169+ Future <String > future2 = Future .of (() -> Util .getSubstringMinusTwo ("Hello" ));
170+ Future <String > errorMessageFuture = future1 .fallbackTo (future2 );
171+ Future <Throwable > errorMessage = errorMessageFuture .failed ();
172+
173+ assertThat (
174+ errorMessage .get ().getMessage ())
175+ .isEqualTo ("String index out of range: -1" );
176+ }
135177}
0 commit comments