From 3a7185206edc912166e3a3672192f9a711f4e4c3 Mon Sep 17 00:00:00 2001 From: Bernardo Bulgarelli Date: Tue, 4 Jul 2023 22:28:02 -0300 Subject: [PATCH] Check for Class and ResolvableType object type attributes See gh-36224 --- .../mock/mockito/MockitoPostProcessor.java | 13 +++++++++- .../mockito/MockitoPostProcessorTests.java | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) 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 f5d0e0ee3e9c..254c18b475f7 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 @@ -254,7 +254,18 @@ private Set 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); } } 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 4e6a0660636f..b5b44d693cd4 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 @@ -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; @@ -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();