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 027f75eae1..73312c4adb 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 @@ -16,6 +16,7 @@ package org.springframework.boot.test.web.client; +import org.springframework.aot.AotDetector; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; @@ -103,6 +104,9 @@ class TestRestTemplateContextCustomizer implements ContextCustomizer { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { + if (AotDetector.useGeneratedArtifacts()) { + return; + } if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors((ListableBeanFactory) this.beanFactory, TestRestTemplate.class, false, false).length == 0) { registry.registerBeanDefinition(TestRestTemplate.class.getName(), 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 7f5217f0ea..bb754700a7 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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.context.ConfigurableApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.test.context.MergedContextConfiguration; @@ -38,13 +39,24 @@ import static org.mockito.Mockito.mock; class TestRestTemplateContextCustomizerTests { @Test - @SuppressWarnings({ "unchecked", "rawtypes" }) void whenContextIsNotABeanDefinitionRegistryTestRestTemplateIsRegistered() { - new ApplicationContextRunner(TestApplicationContext::new).withInitializer((context) -> { - MergedContextConfiguration configuration = mock(MergedContextConfiguration.class); - given(configuration.getTestClass()).willReturn((Class) TestClass.class); - new TestRestTemplateContextCustomizer().customizeContext(context, configuration); - }).run((context) -> assertThat(context).hasSingleBean(TestRestTemplate.class)); + new ApplicationContextRunner(TestApplicationContext::new) + .withInitializer(this::applyTestRestTemplateContextCustomizer) + .run((context) -> assertThat(context).hasSingleBean(TestRestTemplate.class)); + } + + @Test + void whenUsingAotGeneratedArtifactsTestRestTemplateIsNotRegistered() { + new ApplicationContextRunner().withSystemProperties("spring.aot.enabled:true") + .withInitializer(this::applyTestRestTemplateContextCustomizer) + .run((context) -> assertThat(context).doesNotHaveBean(TestRestTemplate.class)); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + void applyTestRestTemplateContextCustomizer(ConfigurableApplicationContext context) { + MergedContextConfiguration configuration = mock(MergedContextConfiguration.class); + given(configuration.getTestClass()).willReturn((Class) TestClass.class); + new TestRestTemplateContextCustomizer().customizeContext(context, configuration); } @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)