2
2
3
3
import com .evanlennick .retry4j .config .RetryConfig ;
4
4
import com .evanlennick .retry4j .config .RetryConfigBuilder ;
5
- import com .evanlennick .retry4j .listener .RetryListener ;
6
- import org .mockito .Mock ;
7
5
import org .mockito .MockitoAnnotations ;
8
6
import org .testng .annotations .BeforeMethod ;
9
7
import org .testng .annotations .Test ;
10
8
11
9
import java .time .temporal .ChronoUnit ;
12
- import java .util .concurrent .Callable ;
10
+ import java .util .ArrayList ;
11
+ import java .util .List ;
13
12
import java .util .concurrent .Executors ;
14
13
15
14
import static org .assertj .core .api .Assertions .assertThat ;
16
- import static org .mockito .Mockito .timeout ;
17
- import static org .mockito .Mockito .verify ;
18
- import static org .mockito .Mockito .when ;
19
15
20
16
public class AsyncCallExecutorTest_ListenersTest {
21
- @ Mock
22
- private DummyMock dummyMock ;
23
17
private AsyncCallExecutor <String > executor ;
24
- private Callable <String > callable ;
25
18
26
19
@ BeforeMethod
27
20
public void setup () {
28
21
MockitoAnnotations .initMocks (this );
29
22
30
- callable = () -> dummyMock .callableCallThis ();
31
-
32
23
RetryConfig config = new RetryConfigBuilder ()
33
24
.retryOnAnyException ()
34
25
.withMaxNumberOfTries (5 )
@@ -41,33 +32,17 @@ public void setup() {
41
32
42
33
@ Test
43
34
public void verifyOnListener_resultHasTypeOfCallExecutor () throws Exception {
44
- when (dummyMock .callableCallThis ())
45
- .thenReturn ("success!" );
46
-
47
- RetryListener <String > listener = status -> {
48
- dummyMock .listenersCallThis ();
49
- assertThat (status .getResult ()).isInstanceOf (String .class );
50
- };
35
+ List <String > methodCalls = new ArrayList <>();
51
36
executor
52
- .onSuccess (listener )
53
- .onCompletion (listener )
54
- .execute (callable ).get ();
55
-
56
- // ensure both listeners are called
57
- verify (dummyMock , timeout (1000 ).times (2 )).listenersCallThis ();
58
- }
59
-
60
- private class DummyMock {
61
- public String listenersCallThis () {
62
- return "this is to use to verify listeners call the mock" ;
63
- }
64
-
65
- public String listenersCallThis (Exception e ) {
66
- return "this is to verify exceptions in the after failed call listener" ;
67
- }
68
-
69
- public String callableCallThis () {
70
- return "this is to use for mocking the executed callable" ;
71
- }
37
+ .onSuccess (status -> {
38
+ methodCalls .add ("onSuccess" );
39
+ assertThat (status .getResult ()).isInstanceOf (String .class );
40
+ })
41
+ .onCompletion (status -> {
42
+ methodCalls .add ("onCompletion" );
43
+ assertThat (status .getResult ()).isInstanceOf (String .class );
44
+ })
45
+ .execute (() -> "" ).get ();
46
+ assertThat (methodCalls ).contains ("onSuccess" , "onCompletion" );
72
47
}
73
48
}
0 commit comments