From c2c6bb25c68c231e8eb250809442987512755d62 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:21:18 +0100 Subject: [PATCH] Use BeanFactory to get type produced by a FactoryBean for Bean Overrides Previously, we only looked at the OBJECT_TYPE_ATTRIBUTE on a FactoryBean's bean definition; however this does not work for situations where the information is provided by the definition's target type rather than the attribute. Rather than manually considering the target type in addition to the existing consideration of the attribute, we now ask the BeanFactory for the type that will be produced by the FactoryBean instead. See https://github.com/spring-projects/spring-boot/issues/40234 Closes gh-33811 Co-authored-by: Andy Wilkinson --- .../bean/override/BeanOverrideBeanFactoryPostProcessor.java | 5 ++--- ...tingGenericBeanProducedByFactoryBeanIntegrationTests.java | 2 -- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java index ba86ecd1fc..fcfb9beea9 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java @@ -286,9 +286,8 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, // Add matching FactoryBeans as well. for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class, true, false)) { beanName = BeanFactoryUtils.transformedBeanName(beanName); - BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); - Object attribute = beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE); - if (resolvableType.equals(attribute) || type.equals(attribute)) { + Class producedType = beanFactory.getType(beanName, false); + if (type.equals(producedType)) { beanNames.add(beanName); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests.java index 790bb703f5..b59df190fa 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests.java @@ -16,7 +16,6 @@ package org.springframework.test.context.bean.override.mockito.integration; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.MockingDetails; @@ -48,7 +47,6 @@ import static org.mockito.Mockito.mockingDetails; * @see MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTests */ @SpringJUnitConfig -@Disabled("Disabled until https://github.com/spring-projects/spring-boot/issues/40234 is ported to Spring Framework") class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests { @MockitoSpyBean("exampleService")