Skip to content

Commit

Permalink
Revert "Remove invalid check for String-based FactoryBean.OBJECT_TYPE…
Browse files Browse the repository at this point in the history
…_ATTRIBUTE"

This reverts commit 279f822.

See gh-36659
  • Loading branch information
wilkinsona committed Aug 1, 2023
1 parent 75bb862 commit d0d5454
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,12 @@ private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory
Set<String> beans = new LinkedHashSet<>(
Arrays.asList(beanFactory.getBeanNamesForType(resolvableType, true, false)));
Class<?> type = resolvableType.resolve(Object.class);
String typeName = type.getName();
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)) {
if (resolvableType.equals(attribute) || type.equals(attribute) || typeName.equals(attribute)) {
beans.add(beanName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ void cannotMockMultipleQualifiedBeans() {
+ " 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
void canMockBeanProducedByFactoryBeanWithClassObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
Expand Down

0 comments on commit d0d5454

Please sign in to comment.