2525package com .iluwatar .async .method .invocation ;
2626
2727import static java .time .Duration .ofMillis ;
28- import static org .junit .jupiter .api .Assertions .assertEquals ;
29- import static org .junit .jupiter .api .Assertions .assertFalse ;
30- import static org .junit .jupiter .api .Assertions .assertNotNull ;
31- import static org .junit .jupiter .api .Assertions .assertSame ;
32- import static org .junit .jupiter .api .Assertions .assertTimeout ;
33- import static org .junit .jupiter .api .Assertions .assertTrue ;
34- import static org .junit .jupiter .api .Assertions .fail ;
35- import static org .mockito .ArgumentMatchers .eq ;
36- import static org .mockito .ArgumentMatchers .isNull ;
28+ import static org .junit .jupiter .api .Assertions .*;
29+ import static org .mockito .ArgumentMatchers .*;
3730import static org .mockito .Mockito .timeout ;
3831import static org .mockito .Mockito .verify ;
3932import static org .mockito .Mockito .verifyNoMoreInteractions ;
5851class ThreadAsyncExecutorTest {
5952
6053 @ Captor
61- private ArgumentCaptor <Optional < Exception >> optionalCaptor ;
54+ private ArgumentCaptor <Exception > exceptionCaptor ;
6255
6356 @ Mock
6457 private Callable <Object > task ;
@@ -118,11 +111,8 @@ void testSuccessfulTaskWithCallback() {
118111 verify (task , times (1 )).call ();
119112
120113 // ... same for the callback, we expect our object
121- verify (callback , times (1 )).onComplete (eq (result ), optionalCaptor .capture ());
122-
123- final var optionalException = optionalCaptor .getValue ();
124- assertNotNull (optionalException );
125- assertFalse (optionalException .isPresent ());
114+ verify (callback , times (1 )).onComplete (eq (result ));
115+ verify (callback , times (0 )).onError (exceptionCaptor .capture ());
126116
127117 // ... and the result should be exactly the same object
128118 assertSame (result , asyncResult .getValue ());
@@ -200,11 +190,8 @@ void testLongRunningTaskWithCallback() {
200190
201191 // Our task should only execute once, but it can take a while ...
202192 verify (task , timeout (3000 ).times (1 )).call ();
203- verify (callback , timeout (3000 ).times (1 )).onComplete (eq (result ), optionalCaptor .capture ());
204-
205- final var optionalException = optionalCaptor .getValue ();
206- assertNotNull (optionalException );
207- assertFalse (optionalException .isPresent ());
193+ verify (callback , timeout (3000 ).times (1 )).onComplete (eq (result ));
194+ verify (callback , times (0 )).onError (isA (Exception .class ));
208195
209196 // Prevent timing issues, and wait until the result is available
210197 asyncResult .await ();
@@ -295,14 +282,12 @@ void testNullTaskWithCallback() {
295282 assertNotNull (asyncResult , "The AsyncResult should not be 'null', even though the task was 'null'." );
296283 asyncResult .await (); // Prevent timing issues, and wait until the result is available
297284 assertTrue (asyncResult .isCompleted ());
298- verify (callback , times (1 )).onComplete (isNull (), optionalCaptor .capture ());
285+ verify (callback , times (0 )).onComplete (any ());
286+ verify (callback , times (1 )).onError (exceptionCaptor .capture ());
299287
300- final var optionalException = optionalCaptor .getValue ();
301- assertNotNull (optionalException );
302- assertTrue (optionalException .isPresent ());
303-
304- final var exception = optionalException .get ();
288+ final var exception = exceptionCaptor .getValue ();
305289 assertNotNull (exception );
290+
306291 assertEquals (NullPointerException .class , exception .getClass ());
307292
308293 try {
@@ -347,4 +332,4 @@ void testNullTaskWithNullCallback() {
347332
348333 }
349334
350- }
335+ }
0 commit comments