Check for Class and ResolvableType object type attributes

See gh-36224
pull/36620/head
Bernardo Bulgarelli 1 year ago committed by Andy Wilkinson
parent fe08c70973
commit 3a7185206e

@ -254,7 +254,18 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
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);
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); beans.add(beanName);
} }
} }

@ -36,6 +36,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -84,6 +85,31 @@ class MockitoPostProcessorTests {
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue(); 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 @Test
void canMockPrimaryBean() { void canMockPrimaryBean() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

Loading…
Cancel
Save