diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizer.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizer.java index 73312c4adbc..550b6125764 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizer.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizer.java @@ -53,6 +53,9 @@ class TestRestTemplateContextCustomizer implements ContextCustomizer { @Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedContextConfiguration) { + if (AotDetector.useGeneratedArtifacts()) { + return; + } SpringBootTest springBootTest = TestContextAnnotationUtils .findMergedAnnotation(mergedContextConfiguration.getTestClass(), SpringBootTest.class); if (springBootTest.webEnvironment().isEmbedded()) { diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizerTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizerTests.java index bb754700a7b..a598ef0cb42 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizerTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizerTests.java @@ -23,6 +23,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer.TestRestTemplateRegistrar; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.test.context.MergedContextConfiguration; @@ -48,8 +49,10 @@ class TestRestTemplateContextCustomizerTests { @Test void whenUsingAotGeneratedArtifactsTestRestTemplateIsNotRegistered() { new ApplicationContextRunner().withSystemProperties("spring.aot.enabled:true") - .withInitializer(this::applyTestRestTemplateContextCustomizer) - .run((context) -> assertThat(context).doesNotHaveBean(TestRestTemplate.class)); + .withInitializer(this::applyTestRestTemplateContextCustomizer).run((context) -> { + assertThat(context).doesNotHaveBean(TestRestTemplateRegistrar.class); + assertThat(context).doesNotHaveBean(TestRestTemplate.class); + }); } @SuppressWarnings({ "unchecked", "rawtypes" })