11package de .codecentric .boot .admin .services ;
22
3+ import static org .mockito .Matchers .eq ;
4+ import static org .mockito .Matchers .isA ;
35import static org .mockito .Mockito .mock ;
46import static org .mockito .Mockito .never ;
57import static org .mockito .Mockito .verify ;
8+ import static org .mockito .Mockito .when ;
9+
10+ import java .util .concurrent .ScheduledFuture ;
611
712import org .junit .Test ;
813import org .springframework .boot .SpringApplication ;
914import org .springframework .boot .context .embedded .EmbeddedWebApplicationContext ;
1015import org .springframework .boot .context .event .ApplicationReadyEvent ;
1116import org .springframework .context .event .ContextClosedEvent ;
17+ import org .springframework .scheduling .TaskScheduler ;
1218
1319public class RegistrationApplicationListenerTest {
1420
1521@ Test
1622public void test_register () throws Exception {
1723ApplicationRegistrator registrator = mock (ApplicationRegistrator .class );
18- RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator );
24+ TaskScheduler scheduler = mock (TaskScheduler .class );
25+ RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator ,
26+ scheduler );
1927
2028listener .onApplicationReady (
2129new ApplicationReadyEvent (mock (SpringApplication .class ), null , null ));
2230
23- Thread .sleep (500 );
24- verify (registrator ).register ();
31+ verify (scheduler ).scheduleAtFixedRate (isA (Runnable .class ), eq (10_000L ));
2532}
2633
2734@ Test
2835public void test_no_register () throws Exception {
2936ApplicationRegistrator registrator = mock (ApplicationRegistrator .class );
30- RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator );
37+ TaskScheduler scheduler = mock (TaskScheduler .class );
38+ RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator ,
39+ scheduler );
3140listener .setAutoRegister (false );
3241
3342listener .onApplicationReady (
3443new ApplicationReadyEvent (mock (SpringApplication .class ), null , null ));
3544
36- Thread .sleep (500 );
37- verify (registrator , never ()).register ();
45+ verify (scheduler , never ()).scheduleAtFixedRate (isA (Runnable .class ), eq (10_000L ));
46+ }
47+
48+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
49+ @ Test
50+ public void test_no_register_after_close () throws Exception {
51+ ApplicationRegistrator registrator = mock (ApplicationRegistrator .class );
52+ TaskScheduler scheduler = mock (TaskScheduler .class );
53+ RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator ,
54+ scheduler );
55+
56+ ScheduledFuture task = mock (ScheduledFuture .class );
57+ when (scheduler .scheduleAtFixedRate (isA (Runnable .class ), eq (10_000L ))).thenReturn (task );
58+
59+ listener .onApplicationReady (
60+ new ApplicationReadyEvent (mock (SpringApplication .class ), null , null ));
61+
62+ verify (scheduler ).scheduleAtFixedRate (isA (Runnable .class ), eq (10_000L ));
63+
64+ listener .onClosedContext (new ContextClosedEvent (mock (EmbeddedWebApplicationContext .class )));
65+ verify (task ).cancel (true );
66+ }
67+
68+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
69+ @ Test
70+ public void test_start_stop () throws Exception {
71+ ApplicationRegistrator registrator = mock (ApplicationRegistrator .class );
72+ TaskScheduler scheduler = mock (TaskScheduler .class );
73+ RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator ,
74+ scheduler );
75+
76+ ScheduledFuture task = mock (ScheduledFuture .class );
77+ when (scheduler .scheduleAtFixedRate (isA (Runnable .class ), eq (10_000L ))).thenReturn (task );
78+
79+ listener .startRegisterTask ();
80+ verify (scheduler ).scheduleAtFixedRate (isA (Runnable .class ), eq (10_000L ));
81+
82+ listener .stopRegisterTask ();
83+ verify (task ).cancel (true );
3884}
3985
4086@ Test
4187public void test_no_deregister () throws Exception {
4288ApplicationRegistrator registrator = mock (ApplicationRegistrator .class );
43- RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator );
89+ TaskScheduler scheduler = mock (TaskScheduler .class );
90+ RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator ,
91+ scheduler );
4492
4593listener .onClosedContext (new ContextClosedEvent (mock (EmbeddedWebApplicationContext .class )));
4694
@@ -50,7 +98,9 @@ public void test_no_deregister() throws Exception {
5098@ Test
5199public void test_deregister () throws Exception {
52100ApplicationRegistrator registrator = mock (ApplicationRegistrator .class );
53- RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator );
101+ TaskScheduler scheduler = mock (TaskScheduler .class );
102+ RegistrationApplicationListener listener = new RegistrationApplicationListener (registrator ,
103+ scheduler );
54104listener .setAutoDeregister (true );
55105
56106listener .onClosedContext (new ContextClosedEvent (mock (EmbeddedWebApplicationContext .class )));
0 commit comments