Skip to content

Commit 34e51dc

Browse files
committed
Polish
1 parent 5b47bf2 commit 34e51dc

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializerAotTests.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
2727
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
2828
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration;
29-
import org.springframework.boot.autoconfigure.AutoConfigurations;
3029
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
31-
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
3230
import org.springframework.boot.test.system.CapturedOutput;
3331
import org.springframework.boot.test.system.OutputCaptureExtension;
3432
import org.springframework.boot.test.util.TestPropertyValues;
@@ -41,6 +39,7 @@
4139
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
4240
import org.springframework.core.test.tools.TestCompiler;
4341
import org.springframework.javapoet.ClassName;
42+
import org.springframework.mock.env.MockEnvironment;
4443
import org.springframework.util.StringUtils;
4544

4645
import 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

Comments
 (0)