2828import org .springframework .boot .WebApplicationType ;
2929import org .springframework .boot .actuate .autoconfigure .endpoint .EndpointAutoConfiguration ;
3030import org .springframework .boot .actuate .autoconfigure .endpoint .web .WebEndpointAutoConfiguration ;
31- import org .springframework .boot .autoconfigure .AutoConfigurations ;
32- import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
3331import org .springframework .boot .test .system .CapturedOutput ;
3432import org .springframework .boot .test .system .OutputCaptureExtension ;
3533import org .springframework .boot .test .util .TestPropertyValues ;
4745import org .springframework .core .test .tools .CompileWithForkedClassLoader ;
4846import org .springframework .core .test .tools .TestCompiler ;
4947import org .springframework .javapoet .ClassName ;
48+ import org .springframework .mock .env .MockEnvironment ;
5049import org .springframework .util .StringUtils ;
5150
5251import static org .assertj .core .api .Assertions .assertThat ;
@@ -64,28 +63,26 @@ class ChildManagementContextInitializerAotTests {
6463 @ CompileWithForkedClassLoader
6564 @ SuppressWarnings ("unchecked" )
6665 void aotContributedInitializerStartsManagementContext (CapturedOutput output ) {
67- WebApplicationContextRunner contextRunner = new WebApplicationContextRunner (
68- AnnotationConfigServletWebServerApplicationContext ::new )
69- .withConfiguration (AutoConfigurations .of (ManagementContextAutoConfiguration .class ,
70- WebEndpointAutoConfiguration .class , EndpointAutoConfiguration .class ))
71- .withUserConfiguration (WebServerConfiguration .class , TestServletManagementContextConfiguration .class );
72- contextRunner .withPropertyValues ("server.port=0" , "management.server.port=0" ).prepare ((context ) -> {
73- TestGenerationContext generationContext = new TestGenerationContext (TestTarget .class );
74- ClassName className = new ApplicationContextAotGenerator ().processAheadOfTime (
75- (GenericApplicationContext ) context .getSourceApplicationContext (), generationContext );
76- generationContext .writeGeneratedContent ();
77- TestCompiler compiler = TestCompiler .forSystem ();
78- compiler .with (generationContext ).compile ((compiled ) -> {
79- ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext ();
80- TestPropertyValues .of ("server.port=0" , "management.server.port=0" ).applyTo (freshApplicationContext );
81- ApplicationContextInitializer <GenericApplicationContext > initializer = compiled
82- .getInstance (ApplicationContextInitializer .class , className .toString ());
83- initializer .initialize (freshApplicationContext );
84- assertThat (output ).satisfies (numberOfOccurrences ("WebServer started" , 0 ));
85- TestPropertyValues .of (AotDetector .AOT_ENABLED + "=true" )
86- .applyToSystemProperties (freshApplicationContext ::refresh );
87- assertThat (output ).satisfies (numberOfOccurrences ("WebServer started" , 2 ));
88- });
66+ AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext ();
67+ context .register (ManagementContextAutoConfiguration .class , WebEndpointAutoConfiguration .class ,
68+ EndpointAutoConfiguration .class );
69+ context .register (WebServerConfiguration .class , TestServletManagementContextConfiguration .class );
70+ context .setEnvironment (
71+ new MockEnvironment ().withProperty ("server.port" , "0" ).withProperty ("management.server.port" , "0" ));
72+ TestGenerationContext generationContext = new TestGenerationContext (TestTarget .class );
73+ ClassName className = new ApplicationContextAotGenerator ().processAheadOfTime (context , generationContext );
74+ generationContext .writeGeneratedContent ();
75+ TestCompiler compiler = TestCompiler .forSystem ();
76+ compiler .with (generationContext ).compile ((compiled ) -> {
77+ ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext ();
78+ TestPropertyValues .of ("server.port=0" , "management.server.port=0" ).applyTo (freshApplicationContext );
79+ ApplicationContextInitializer <GenericApplicationContext > initializer = compiled
80+ .getInstance (ApplicationContextInitializer .class , className .toString ());
81+ initializer .initialize (freshApplicationContext );
82+ assertThat (output ).satisfies (numberOfOccurrences ("WebServer started" , 0 ));
83+ TestPropertyValues .of (AotDetector .AOT_ENABLED + "=true" )
84+ .applyToSystemProperties (freshApplicationContext ::refresh );
85+ assertThat (output ).satisfies (numberOfOccurrences ("WebServer started" , 2 ));
8986 });
9087 }
9188
0 commit comments