From 82b27c60a4268236f05462ca7f56e0b8d168b48a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 5 Sep 2018 16:10:32 -0700 Subject: [PATCH 1/4] Drop MockitoPostProcessor factory method Remove factory method previously used to create the mock instance. Since commit 0e00a49dcc, the method is not longer needed. See gh-11077 --- .../mock/mockito/MockitoPostProcessor.java | 18 +----------------- 1 file changed, 1 insertion(+), 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 1bafe45919..91f4b8cdac 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 @@ -183,13 +183,11 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda RootBeanDefinition beanDefinition = createBeanDefinition(definition); String beanName = getBeanName(beanFactory, registry, definition, beanDefinition); String transformedBeanName = BeanFactoryUtils.transformedBeanName(beanName); - beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(1, - beanName); if (registry.containsBeanDefinition(transformedBeanName)) { registry.removeBeanDefinition(transformedBeanName); } registry.registerBeanDefinition(transformedBeanName, beanDefinition); - Object mock = createMock(definition, beanName); + Object mock = definition.createMock(beanName + " bean"); beanFactory.registerSingleton(transformedBeanName, mock); this.mockitoBeans.add(mock); this.beanNameRegistry.put(definition, beanName); @@ -202,26 +200,12 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda RootBeanDefinition definition = new RootBeanDefinition( mockDefinition.getTypeToMock().resolve()); definition.setTargetType(mockDefinition.getTypeToMock()); - definition.setFactoryBeanName(BEAN_NAME); - definition.setFactoryMethodName("createMock"); - definition.getConstructorArgumentValues().addIndexedArgumentValue(0, - mockDefinition); if (mockDefinition.getQualifier() != null) { mockDefinition.getQualifier().applyTo(definition); } return definition; } - /** - * Factory method used by defined beans to actually create the mock. - * @param mockDefinition the mock definition - * @param name the bean name - * @return the mock instance - */ - protected final Object createMock(MockDefinition mockDefinition, String name) { - return mockDefinition.createMock(name + " bean"); - } - private String getBeanName(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry, MockDefinition mockDefinition, RootBeanDefinition beanDefinition) { From c777614d8f3a1072b9e3d03acf6adf2f766f9e48 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 5 Sep 2018 16:30:05 -0700 Subject: [PATCH 2/4] Support @MockBean/@SpyBean with @Primary Update `MockitoPostProcessor` so that `@MockBean` and `@SpyBean` work consistently when combined with `@Primary`. See gh-11077 Co-authored-by: Andreas Neiser --- .../mock/mockito/MockitoPostProcessor.java | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 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 91f4b8cdac..82112e6bc5 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 @@ -19,6 +19,7 @@ package org.springframework.boot.test.mock.mockito; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -184,6 +185,8 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda String beanName = getBeanName(beanFactory, registry, definition, beanDefinition); String transformedBeanName = BeanFactoryUtils.transformedBeanName(beanName); if (registry.containsBeanDefinition(transformedBeanName)) { + BeanDefinition existing = registry.getBeanDefinition(transformedBeanName); + copyBeanDefinitionDetails(existing, beanDefinition); registry.removeBeanDefinition(transformedBeanName); } registry.registerBeanDefinition(transformedBeanName, beanDefinition); @@ -196,6 +199,10 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda } } + private void copyBeanDefinitionDetails(BeanDefinition from, RootBeanDefinition to) { + to.setPrimary(from.isPrimary()); + } + private RootBeanDefinition createBeanDefinition(MockDefinition mockDefinition) { RootBeanDefinition definition = new RootBeanDefinition( mockDefinition.getTypeToMock().resolve()); @@ -212,13 +219,19 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda if (StringUtils.hasLength(mockDefinition.getName())) { return mockDefinition.getName(); } - Set existingBeans = findCandidateBeans(beanFactory, mockDefinition); + Set existingBeans = findCandidateBeans(beanFactory, + mockDefinition.getTypeToMock(), mockDefinition.getQualifier()); if (existingBeans.isEmpty()) { return this.beanNameGenerator.generateBeanName(beanDefinition, registry); } if (existingBeans.size() == 1) { return existingBeans.iterator().next(); } + String primaryCandidate = determinePrimaryCandidate(registry, existingBeans, + mockDefinition.getTypeToMock()); + if (primaryCandidate != null) { + return primaryCandidate; + } throw new IllegalStateException( "Unable to register mock bean " + mockDefinition.getTypeToMock() + " expected a single matching bean to replace but found " @@ -226,22 +239,21 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda } private void registerSpy(ConfigurableListableBeanFactory beanFactory, - BeanDefinitionRegistry registry, SpyDefinition definition, Field field) { - String[] existingBeans = getExistingBeans(beanFactory, definition.getTypeToSpy()); + BeanDefinitionRegistry registry, SpyDefinition spyDefinition, Field field) { + Set existingBeans = findCandidateBeans(beanFactory, + spyDefinition.getTypeToSpy(), spyDefinition.getQualifier()); if (ObjectUtils.isEmpty(existingBeans)) { - createSpy(registry, definition, field); + createSpy(registry, spyDefinition, field); } else { - registerSpies(registry, definition, field, existingBeans); + registerSpies(registry, spyDefinition, field, existingBeans); } } private Set findCandidateBeans(ConfigurableListableBeanFactory beanFactory, - MockDefinition mockDefinition) { - QualifierDefinition qualifier = mockDefinition.getQualifier(); + ResolvableType type, QualifierDefinition qualifier) { Set candidates = new TreeSet<>(); - for (String candidate : getExistingBeans(beanFactory, - mockDefinition.getTypeToMock())) { + for (String candidate : getExistingBeans(beanFactory, type)) { if (qualifier == null || qualifier.matches(beanFactory, candidate)) { candidates.add(candidate); } @@ -249,7 +261,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda return candidates; } - private String[] getExistingBeans(ConfigurableListableBeanFactory beanFactory, + private Set getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType type) { Set beans = new LinkedHashSet<>( Arrays.asList(beanFactory.getBeanNamesForType(type))); @@ -263,7 +275,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda } } beans.removeIf(this::isScopedTarget); - return StringUtils.toStringArray(beans); + return beans; } private boolean isScopedTarget(String beanName) { @@ -275,49 +287,49 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda } } - private void createSpy(BeanDefinitionRegistry registry, SpyDefinition definition, + private void createSpy(BeanDefinitionRegistry registry, SpyDefinition spyDefinition, Field field) { RootBeanDefinition beanDefinition = new RootBeanDefinition( - definition.getTypeToSpy().resolve()); + spyDefinition.getTypeToSpy().resolve()); String beanName = this.beanNameGenerator.generateBeanName(beanDefinition, registry); registry.registerBeanDefinition(beanName, beanDefinition); - registerSpy(definition, field, beanName); + registerSpy(spyDefinition, field, beanName); } - private void registerSpies(BeanDefinitionRegistry registry, SpyDefinition definition, - Field field, String[] existingBeans) { + private void registerSpies(BeanDefinitionRegistry registry, + SpyDefinition spyDefinition, Field field, Collection existingBeans) { try { - registerSpy(definition, field, - determineBeanName(existingBeans, definition, registry)); + String beanName = determineBeanName(existingBeans, spyDefinition, registry); + registerSpy(spyDefinition, field, beanName); } catch (RuntimeException ex) { throw new IllegalStateException( - "Unable to register spy bean " + definition.getTypeToSpy(), ex); + "Unable to register spy bean " + spyDefinition.getTypeToSpy(), ex); } } - private String determineBeanName(String[] existingBeans, SpyDefinition definition, - BeanDefinitionRegistry registry) { + private String determineBeanName(Collection existingBeans, + SpyDefinition definition, BeanDefinitionRegistry registry) { if (StringUtils.hasText(definition.getName())) { return definition.getName(); } - if (existingBeans.length == 1) { - return existingBeans[0]; + if (existingBeans.size() == 1) { + return existingBeans.iterator().next(); } return determinePrimaryCandidate(registry, existingBeans, definition.getTypeToSpy()); } private String determinePrimaryCandidate(BeanDefinitionRegistry registry, - String[] candidateBeanNames, ResolvableType type) { + Collection candidateBeanNames, ResolvableType type) { String primaryBeanName = null; for (String candidateBeanName : candidateBeanNames) { BeanDefinition beanDefinition = registry.getBeanDefinition(candidateBeanName); if (beanDefinition.isPrimary()) { if (primaryBeanName != null) { throw new NoUniqueBeanDefinitionException(type.resolve(), - candidateBeanNames.length, + candidateBeanNames.size(), "more than one 'primary' bean found among candidates: " + Arrays.asList(candidateBeanNames)); } From a5b3a2646bd90bde4fd9b8fa86a3cddaac7ed844 Mon Sep 17 00:00:00 2001 From: Andreas Neiser Date: Wed, 5 Sep 2018 14:14:14 -0700 Subject: [PATCH 3/4] Test @MockBean/@SpyBean with @Primary Add additional tests to ensure that `@MockBean` and `@SpyBean` work consistently when combined with `@Primary`. See gh-11077 --- .../mockito/MockitoPostProcessorTests.java | 159 +++++++++++++++++- ...tingBeanWithQualifierIntegrationTests.java | 89 ++++++++++ 2 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests.java 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 1a3c426b5b..35cf78c1cb 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,9 +26,11 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.boot.test.mock.mockito.example.ExampleService; import org.springframework.boot.test.mock.mockito.example.FailingExampleService; +import org.springframework.boot.test.mock.mockito.example.RealExampleService; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; import static org.assertj.core.api.Assertions.assertThat; @@ -84,6 +86,79 @@ public class MockitoPostProcessorTests { .isTrue(); } + @Test + public void canMockPrimaryBean() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + MockitoPostProcessor.register(context); + context.register(MockPrimaryBean.class); + context.refresh(); + assertThat(Mockito.mockingDetails(context.getBean(MockPrimaryBean.class).mock) + .isMock()).isTrue(); + assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isMock()) + .isTrue(); + assertThat(Mockito + .mockingDetails(context.getBean("examplePrimary", ExampleService.class)) + .isMock()).isTrue(); + assertThat(Mockito + .mockingDetails(context.getBean("exampleQualified", ExampleService.class)) + .isMock()).isFalse(); + } + + @Test + public void canMockQualifiedBeanWithPrimaryBeanPresent() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + MockitoPostProcessor.register(context); + context.register(MockQualifiedBean.class); + context.refresh(); + assertThat(Mockito.mockingDetails(context.getBean(MockQualifiedBean.class).mock) + .isMock()).isTrue(); + assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isMock()) + .isFalse(); + assertThat(Mockito + .mockingDetails(context.getBean("examplePrimary", ExampleService.class)) + .isMock()).isFalse(); + assertThat(Mockito + .mockingDetails(context.getBean("exampleQualified", ExampleService.class)) + .isMock()).isTrue(); + } + + @Test + public void canSpyPrimaryBean() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + MockitoPostProcessor.register(context); + context.register(SpyPrimaryBean.class); + context.refresh(); + assertThat( + Mockito.mockingDetails(context.getBean(SpyPrimaryBean.class).spy).isSpy()) + .isTrue(); + assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isSpy()) + .isTrue(); + assertThat(Mockito + .mockingDetails(context.getBean("examplePrimary", ExampleService.class)) + .isSpy()).isTrue(); + assertThat(Mockito + .mockingDetails(context.getBean("exampleQualified", ExampleService.class)) + .isSpy()).isFalse(); + } + + @Test + public void canSpyQualifiedBeanWithPrimaryBeanPresent() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + MockitoPostProcessor.register(context); + context.register(SpyQualifiedBean.class); + context.refresh(); + assertThat(Mockito.mockingDetails(context.getBean(SpyQualifiedBean.class).spy) + .isSpy()).isTrue(); + assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isSpy()) + .isFalse(); + assertThat(Mockito + .mockingDetails(context.getBean("examplePrimary", ExampleService.class)) + .isSpy()).isFalse(); + assertThat(Mockito + .mockingDetails(context.getBean("exampleQualified", ExampleService.class)) + .isSpy()).isTrue(); + } + @Configuration @MockBean(SomeInterface.class) static class MockedFactoryBean { @@ -137,6 +212,88 @@ public class MockitoPostProcessorTests { } + @Configuration + static class MockPrimaryBean { + + @MockBean(ExampleService.class) + private ExampleService mock; + + @Bean + @Qualifier("test") + public ExampleService exampleQualified() { + return new RealExampleService("qualified"); + } + + @Bean + @Primary + public ExampleService examplePrimary() { + return new RealExampleService("primary"); + } + + } + + @Configuration + static class MockQualifiedBean { + + @MockBean(ExampleService.class) + @Qualifier("test") + private ExampleService mock; + + @Bean + @Qualifier("test") + public ExampleService exampleQualified() { + return new RealExampleService("qualified"); + } + + @Bean + @Primary + public ExampleService examplePrimary() { + return new RealExampleService("primary"); + } + + } + + @Configuration + static class SpyPrimaryBean { + + @SpyBean(ExampleService.class) + private ExampleService spy; + + @Bean + @Qualifier("test") + public ExampleService exampleQualified() { + return new RealExampleService("qualified"); + } + + @Bean + @Primary + public ExampleService examplePrimary() { + return new RealExampleService("primary"); + } + + } + + @Configuration + static class SpyQualifiedBean { + + @SpyBean(ExampleService.class) + @Qualifier("test") + private ExampleService spy; + + @Bean + @Qualifier("test") + public ExampleService exampleQualified() { + return new RealExampleService("qualified"); + } + + @Bean + @Primary + public ExampleService examplePrimary() { + return new RealExampleService("primary"); + } + + } + static class TestFactoryBean implements FactoryBean { @Override diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests.java new file mode 100644 index 0000000000..d60f206a45 --- /dev/null +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests.java @@ -0,0 +1,89 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.mock.mockito; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.example.CustomQualifier; +import org.springframework.boot.test.mock.mockito.example.CustomQualifierExampleService; +import org.springframework.boot.test.mock.mockito.example.ExampleService; +import org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller; +import org.springframework.boot.test.mock.mockito.example.RealExampleService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.verify; + +/** + * Test {@link SpyBean} on a test class field can be used to replace existing bean while + * preserving qualifiers. + * + * @author Andreas Neiser + */ +@RunWith(SpringRunner.class) +public class SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests { + + @SpyBean + @CustomQualifier + private ExampleService service; + + @Autowired + private ExampleServiceCaller caller; + + @Autowired + private ApplicationContext applicationContext; + + @Test + public void testMocking() throws Exception { + this.caller.sayGreeting(); + verify(this.service).greeting(); + } + + @Test + public void onlyQualifiedBeanIsReplaced() { + assertThat(this.applicationContext.getBean("service")).isSameAs(this.service); + ExampleService anotherService = this.applicationContext.getBean("anotherService", + ExampleService.class); + assertThat(anotherService.greeting()).isEqualTo("Another"); + } + + @Configuration + static class TestConfig { + + @Bean + public CustomQualifierExampleService service() { + return new CustomQualifierExampleService(); + } + + @Bean + public ExampleService anotherService() { + return new RealExampleService("Another"); + } + + @Bean + public ExampleServiceCaller controller(@CustomQualifier ExampleService service) { + return new ExampleServiceCaller(service); + } + + } + +} From bc357225b57f442c6988a5f16a7aa9b53790a866 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 6 Sep 2018 12:47:25 -0700 Subject: [PATCH 4/4] Polish MockitoPostProcessor Closes gh-11077 --- .../mock/mockito/MockitoPostProcessor.java | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 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 82112e6bc5..851ca6fcec 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 @@ -86,14 +86,14 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda .getQualifiedAttributeName(ConfigurationClassPostProcessor.class, "configurationClass"); + private static final BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator(); + private final Set definitions; private ClassLoader classLoader; private BeanFactory beanFactory; - private final BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator(); - private final MockitoBeans mockitoBeans = new MockitoBeans(); private Map beanNameRegistry = new HashMap<>(); @@ -199,10 +199,6 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda } } - private void copyBeanDefinitionDetails(BeanDefinition from, RootBeanDefinition to) { - to.setPrimary(from.isPrimary()); - } - private RootBeanDefinition createBeanDefinition(MockDefinition mockDefinition) { RootBeanDefinition definition = new RootBeanDefinition( mockDefinition.getTypeToMock().resolve()); @@ -219,10 +215,11 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda if (StringUtils.hasLength(mockDefinition.getName())) { return mockDefinition.getName(); } - Set existingBeans = findCandidateBeans(beanFactory, + Set existingBeans = getExistingBeans(beanFactory, mockDefinition.getTypeToMock(), mockDefinition.getQualifier()); if (existingBeans.isEmpty()) { - return this.beanNameGenerator.generateBeanName(beanDefinition, registry); + return MockitoPostProcessor.beanNameGenerator.generateBeanName(beanDefinition, + registry); } if (existingBeans.size() == 1) { return existingBeans.iterator().next(); @@ -238,9 +235,13 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda + existingBeans); } + private void copyBeanDefinitionDetails(BeanDefinition from, RootBeanDefinition to) { + to.setPrimary(from.isPrimary()); + } + private void registerSpy(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry, SpyDefinition spyDefinition, Field field) { - Set existingBeans = findCandidateBeans(beanFactory, + Set existingBeans = getExistingBeans(beanFactory, spyDefinition.getTypeToSpy(), spyDefinition.getQualifier()); if (ObjectUtils.isEmpty(existingBeans)) { createSpy(registry, spyDefinition, field); @@ -250,7 +251,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda } } - private Set findCandidateBeans(ConfigurableListableBeanFactory beanFactory, + private Set getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType type, QualifierDefinition qualifier) { Set candidates = new TreeSet<>(); for (String candidate : getExistingBeans(beanFactory, type)) { @@ -265,12 +266,11 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda ResolvableType type) { Set beans = new LinkedHashSet<>( Arrays.asList(beanFactory.getBeanNamesForType(type))); - String resolvedTypeName = type.resolve(Object.class).getName(); + String typeName = type.resolve(Object.class).getName(); for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class)) { beanName = BeanFactoryUtils.transformedBeanName(beanName); BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); - if (resolvedTypeName - .equals(beanDefinition.getAttribute(FACTORY_BEAN_OBJECT_TYPE))) { + if (typeName.equals(beanDefinition.getAttribute(FACTORY_BEAN_OBJECT_TYPE))) { beans.add(beanName); } } @@ -291,8 +291,8 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda Field field) { RootBeanDefinition beanDefinition = new RootBeanDefinition( spyDefinition.getTypeToSpy().resolve()); - String beanName = this.beanNameGenerator.generateBeanName(beanDefinition, - registry); + String beanName = MockitoPostProcessor.beanNameGenerator + .generateBeanName(beanDefinition, registry); registry.registerBeanDefinition(beanName, beanDefinition); registerSpy(spyDefinition, field, beanName); } @@ -347,7 +347,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda } } - protected Object createSpyIfNecessary(Object bean, String beanName) + protected final Object createSpyIfNecessary(Object bean, String beanName) throws BeansException { SpyDefinition definition = this.spies.get(beanName); if (definition != null) { @@ -476,7 +476,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda @Override public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException { - return createSpyIfNecessary(bean, beanName); + return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName); } @Override @@ -485,10 +485,6 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda if (bean instanceof FactoryBean) { return bean; } - return createSpyIfNecessary(bean, beanName); - } - - private Object createSpyIfNecessary(Object bean, String beanName) { return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName); }