mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge pull request #36224 from bbulgarelli
* gh-36224: Polish "Check for Class and ResolvableType object type attributes" Check for Class and ResolvableType object type attributes Closes gh-36224
This commit is contained in:
commit
300e50721f
@ -248,18 +248,22 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
|
|||||||
return candidates;
|
return candidates;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType type) {
|
private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType resolvableType) {
|
||||||
Set<String> beans = new LinkedHashSet<>(Arrays.asList(beanFactory.getBeanNamesForType(type, true, false)));
|
Set<String> beans = new LinkedHashSet<>(
|
||||||
String typeName = type.resolve(Object.class).getName();
|
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)) {
|
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 (resolvableType.equals(attribute) || type.equals(attribute) || typeName.equals(attribute)) {
|
||||||
beans.add(beanName);
|
beans.add(beanName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
beans.removeIf(this::isScopedTarget);
|
beans.removeIf(this::isScopedTarget);
|
||||||
return beans;
|
return beans;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isScopedTarget(String beanName) {
|
private boolean isScopedTarget(String 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;
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ class MockitoPostProcessorTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void canMockBeanProducedByFactoryBeanWithObjectTypeAttribute() {
|
void canMockBeanProducedByFactoryBeanWithStringObjectTypeAttribute() {
|
||||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||||
MockitoPostProcessor.register(context);
|
MockitoPostProcessor.register(context);
|
||||||
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
|
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
|
||||||
@ -84,6 +85,31 @@ class MockitoPostProcessorTests {
|
|||||||
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue();
|
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void canMockBeanProducedByFactoryBeanWithClassObjectTypeAttribute() {
|
||||||
|
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 canMockBeanProducedByFactoryBeanWithResolvableTypeObjectTypeAttribute() {
|
||||||
|
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…
Reference in New Issue
Block a user