Revert "Remove invalid check for String-based FactoryBean.OBJECT_TYPE_ATTRIBUTE"

This reverts commit 279f8221a5.

See gh-36659
pull/36682/head
Andy Wilkinson 1 year ago
parent 75bb862255
commit d0d545468a

@ -252,11 +252,12 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
Set<String> beans = new LinkedHashSet<>( Set<String> beans = new LinkedHashSet<>(
Arrays.asList(beanFactory.getBeanNamesForType(resolvableType, true, false))); Arrays.asList(beanFactory.getBeanNamesForType(resolvableType, true, false)));
Class<?> type = resolvableType.resolve(Object.class); Class<?> type = resolvableType.resolve(Object.class);
String typeName = type.getName();
for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class, true, false)) { for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class, true, false)) {
beanName = BeanFactoryUtils.transformedBeanName(beanName); beanName = BeanFactoryUtils.transformedBeanName(beanName);
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
Object attribute = beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE); Object attribute = beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
if (resolvableType.equals(attribute) || type.equals(attribute)) { if (resolvableType.equals(attribute) || type.equals(attribute) || typeName.equals(attribute)) {
beans.add(beanName); beans.add(beanName);
} }
} }

@ -73,6 +73,18 @@ class MockitoPostProcessorTests {
+ " expected a single matching bean to replace but found [example1, example3]"); + " expected a single matching bean to replace but found [example1, example3]");
} }
@Test
void canMockBeanProducedByFactoryBeanWithStringObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
factoryBeanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, SomeInterface.class.getName());
context.registerBeanDefinition("beanToBeMocked", factoryBeanDefinition);
context.register(MockedFactoryBean.class);
context.refresh();
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue();
}
@Test @Test
void canMockBeanProducedByFactoryBeanWithClassObjectTypeAttribute() { void canMockBeanProducedByFactoryBeanWithClassObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

Loading…
Cancel
Save