Prevent duplicate registration of TestRestTemplate after AOT processing

Closes gh-32542
pull/32627/head
Andy Wilkinson 2 years ago
parent 188cac6540
commit 68e4aa232b

@ -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(),

@ -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) -> {
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);
}).run((context) -> assertThat(context).hasSingleBean(TestRestTemplate.class));
}
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)

Loading…
Cancel
Save