Skip to content

Commit

Permalink
Check for Class and ResolvableType object type attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbulgarelli authored and wilkinsona committed Jul 5, 2023
1 parent fe08c70 commit 3a71852
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,18 @@ private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory
for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class, true, false)) {
beanName = BeanFactoryUtils.transformedBeanName(beanName);
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
if (typeName.equals(beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE))) {
Object attribute = beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
if(attribute instanceof Class) {
Class<?> attributeClass = (Class<?>) attribute;
if (typeName.equals(attributeClass.getName())) {
beans.add(beanName);
}
} else if (attribute instanceof ResolvableType) {
ResolvableType resolvableType = (ResolvableType) attribute;
if (typeName.equals(resolvableType.resolve(Object.class).getName())) {
beans.add(beanName);
}
} else if (typeName.equals(attribute)){
beans.add(beanName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -84,6 +85,31 @@ void canMockBeanProducedByFactoryBeanWithObjectTypeAttribute() {
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue();
}

@Test
void canMockBeanProducedByFactoryBeanWithClassAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
factoryBeanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, SomeInterface.class);
context.registerBeanDefinition("beanToBeMocked", factoryBeanDefinition);
context.register(MockedFactoryBean.class);
context.refresh();
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue();
}

@Test
void canMockBeanProducedByFactoryBeanWithResolvableTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
ResolvableType objectType = ResolvableType.forClass(SomeInterface.class);
factoryBeanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, objectType);
context.registerBeanDefinition("beanToBeMocked", factoryBeanDefinition);
context.register(MockedFactoryBean.class);
context.refresh();
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue();
}

@Test
void canMockPrimaryBean() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
Expand Down

0 comments on commit 3a71852

Please sign in to comment.