File tree Expand file tree Collapse file tree
main/java/org/baeldung/startup
test/java/org/baeldung/startup Expand file tree Collapse file tree Original file line number Diff line number Diff line change 130130 <scope >test</scope >
131131 </dependency >
132132
133+ <dependency >
134+ <groupId >org.assertj</groupId >
135+ <artifactId >assertj-core</artifactId >
136+ <version >3.5.1</version >
137+ <scope >test</scope >
138+ </dependency >
139+
133140 <dependency >
134141 <groupId >org.hamcrest</groupId >
135142 <artifactId >hamcrest-core</artifactId >
Original file line number Diff line number Diff line change 1+ package org .baeldung .startup ;
2+
3+ import org .springframework .context .ApplicationListener ;
4+ import org .springframework .context .event .ContextRefreshedEvent ;
5+ import org .springframework .stereotype .Component ;
6+
7+ @ Component
8+ public class StartupApplicationListenerExample implements ApplicationListener <ContextRefreshedEvent > {
9+
10+ public static int counter ;
11+
12+ @ Override
13+ public void onApplicationEvent (ContextRefreshedEvent event ) {
14+ counter ++;
15+ }
16+ }
Original file line number Diff line number Diff line change 11package org .baeldung .startup ;
22
3+ import org .assertj .core .api .Assertions ;
34import org .junit .Test ;
45import org .junit .runner .RunWith ;
56import org .springframework .beans .factory .BeanCreationException ;
@@ -35,4 +36,9 @@ public void whenConstructorInjection_shouldLogEnv() throws Exception {
3536 public void whenInitializingBean_shouldLogEnv () throws Exception {
3637 ctx .getBean (InitializingBeanExampleBean .class );
3738 }
39+
40+ @ Test
41+ public void whenApplicationListener_shouldRunOnce () throws Exception {
42+ Assertions .assertThat (StartupApplicationListenerExample .counter ).isEqualTo (1 );
43+ }
3844}
You can’t perform that action at this time.
0 commit comments