2626import org .springframework .boot .actuate .autoconfigure .endpoint .EndpointAutoConfiguration ;
2727import org .springframework .boot .actuate .autoconfigure .endpoint .web .WebEndpointAutoConfiguration ;
2828import org .springframework .boot .actuate .autoconfigure .web .servlet .ServletManagementContextAutoConfiguration ;
29- import org .springframework .boot .autoconfigure .AutoConfigurations ;
3029import org .springframework .boot .autoconfigure .web .servlet .ServletWebServerFactoryAutoConfiguration ;
31- import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
3230import org .springframework .boot .test .system .CapturedOutput ;
3331import org .springframework .boot .test .system .OutputCaptureExtension ;
3432import org .springframework .boot .test .util .TestPropertyValues ;
4139import org .springframework .core .test .tools .CompileWithForkedClassLoader ;
4240import org .springframework .core .test .tools .TestCompiler ;
4341import org .springframework .javapoet .ClassName ;
42+ import org .springframework .mock .env .MockEnvironment ;
4443import org .springframework .util .StringUtils ;
4544
4645import static org .assertj .core .api .Assertions .assertThat ;
@@ -58,28 +57,26 @@ class ChildManagementContextInitializerAotTests {
5857 @ CompileWithForkedClassLoader
5958 @ SuppressWarnings ("unchecked" )
6059 void aotContributedInitializerStartsManagementContext (CapturedOutput output ) {
61- WebApplicationContextRunner contextRunner = new WebApplicationContextRunner (
62- AnnotationConfigServletWebServerApplicationContext ::new )
63- .withConfiguration (AutoConfigurations .of (ManagementContextAutoConfiguration .class ,
64- ServletWebServerFactoryAutoConfiguration .class , ServletManagementContextAutoConfiguration .class ,
65- WebEndpointAutoConfiguration .class , EndpointAutoConfiguration .class ));
66- contextRunner .withPropertyValues ("server.port=0" , "management.server.port=0" ).prepare ((context ) -> {
67- TestGenerationContext generationContext = new TestGenerationContext (TestTarget .class );
68- ClassName className = new ApplicationContextAotGenerator ().processAheadOfTime (
69- (GenericApplicationContext ) context .getSourceApplicationContext (), generationContext );
70- generationContext .writeGeneratedContent ();
71- TestCompiler compiler = TestCompiler .forSystem ();
72- compiler .with (generationContext ).compile ((compiled ) -> {
73- ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext ();
74- TestPropertyValues .of ("server.port=0" , "management.server.port=0" ).applyTo (freshApplicationContext );
75- ApplicationContextInitializer <GenericApplicationContext > initializer = compiled
76- .getInstance (ApplicationContextInitializer .class , className .toString ());
77- initializer .initialize (freshApplicationContext );
78- assertThat (output ).satisfies (numberOfOccurrences ("Tomcat started on port" , 0 ));
79- TestPropertyValues .of (AotDetector .AOT_ENABLED + "=true" )
80- .applyToSystemProperties (freshApplicationContext ::refresh );
81- assertThat (output ).satisfies (numberOfOccurrences ("Tomcat started on port" , 2 ));
82- });
60+ AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext ();
61+ context .register (ManagementContextAutoConfiguration .class , ServletWebServerFactoryAutoConfiguration .class ,
62+ ServletManagementContextAutoConfiguration .class , WebEndpointAutoConfiguration .class ,
63+ EndpointAutoConfiguration .class );
64+ context .setEnvironment (
65+ new MockEnvironment ().withProperty ("server.port" , "0" ).withProperty ("management.server.port" , "0" ));
66+ TestGenerationContext generationContext = new TestGenerationContext (TestTarget .class );
67+ ClassName className = new ApplicationContextAotGenerator ().processAheadOfTime (context , generationContext );
68+ generationContext .writeGeneratedContent ();
69+ TestCompiler compiler = TestCompiler .forSystem ();
70+ compiler .with (generationContext ).compile ((compiled ) -> {
71+ ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext ();
72+ TestPropertyValues .of ("server.port=0" , "management.server.port=0" ).applyTo (freshApplicationContext );
73+ ApplicationContextInitializer <GenericApplicationContext > initializer = compiled
74+ .getInstance (ApplicationContextInitializer .class , className .toString ());
75+ initializer .initialize (freshApplicationContext );
76+ assertThat (output ).satisfies (numberOfOccurrences ("Tomcat started on port" , 0 ));
77+ TestPropertyValues .of (AotDetector .AOT_ENABLED + "=true" )
78+ .applyToSystemProperties (freshApplicationContext ::refresh );
79+ assertThat (output ).satisfies (numberOfOccurrences ("Tomcat started on port" , 2 ));
8380 });
8481 }
8582
0 commit comments