From f4c996b4324bb127ef26a63a9306d0bf133519c3 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 5 Jul 2023 09:04:58 +0100 Subject: [PATCH] Polish "Check for Class and ResolvableType object type attributes" See gh-36224 --- .../mock/mockito/MockitoPostProcessor.java | 21 +++++++------------ .../mockito/MockitoPostProcessorTests.java | 6 +++--- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java index 254c18b475..ec66aa12ff 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java @@ -248,29 +248,22 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor return candidates; } - private Set getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType type) { - Set beans = new LinkedHashSet<>(Arrays.asList(beanFactory.getBeanNamesForType(type, true, false))); - String typeName = type.resolve(Object.class).getName(); + private Set getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType resolvableType) { + Set 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(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)){ + if (resolvableType.equals(attribute) || type.equals(attribute) || typeName.equals(attribute)) { beans.add(beanName); } } beans.removeIf(this::isScopedTarget); return beans; + } private boolean isScopedTarget(String beanName) { diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java index b5b44d693c..99969dadd9 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java @@ -74,7 +74,7 @@ class MockitoPostProcessorTests { } @Test - void canMockBeanProducedByFactoryBeanWithObjectTypeAttribute() { + void canMockBeanProducedByFactoryBeanWithStringObjectTypeAttribute() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); MockitoPostProcessor.register(context); RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class); @@ -86,7 +86,7 @@ class MockitoPostProcessorTests { } @Test - void canMockBeanProducedByFactoryBeanWithClassAttribute() { + void canMockBeanProducedByFactoryBeanWithClassObjectTypeAttribute() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); MockitoPostProcessor.register(context); RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class); @@ -98,7 +98,7 @@ class MockitoPostProcessorTests { } @Test - void canMockBeanProducedByFactoryBeanWithResolvableTypeAttribute() { + void canMockBeanProducedByFactoryBeanWithResolvableTypeObjectTypeAttribute() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); MockitoPostProcessor.register(context); RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);