Skip to content

Commit 21bb249

Browse files
committed
Merge branch '2.2.x'
Closes gh-21090
2 parents 56711d6 + b9c2b7b commit 21bb249

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -248,9 +248,9 @@ private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory
248248
}
249249

250250
private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType type) {
251-
Set<String> beans = new LinkedHashSet<>(Arrays.asList(beanFactory.getBeanNamesForType(type)));
251+
Set<String> beans = new LinkedHashSet<>(Arrays.asList(beanFactory.getBeanNamesForType(type, true, false)));
252252
String typeName = type.resolve(Object.class).getName();
253-
for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class)) {
253+
for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class, true, false)) {
254254
beanName = BeanFactoryUtils.transformedBeanName(beanName);
255255
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
256256
if (typeName.equals(beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE))) {

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,18 @@
1616

1717
package org.springframework.boot.test.mock.mockito;
1818

19+
import java.util.Map;
20+
1921
import org.junit.jupiter.api.Test;
2022
import org.mockito.Mockito;
2123

24+
import org.springframework.beans.BeanWrapper;
25+
import org.springframework.beans.BeansException;
2226
import org.springframework.beans.factory.FactoryBean;
2327
import org.springframework.beans.factory.annotation.Qualifier;
28+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
29+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
30+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2431
import org.springframework.beans.factory.support.RootBeanDefinition;
2532
import org.springframework.boot.test.mock.mockito.example.ExampleService;
2633
import org.springframework.boot.test.mock.mockito.example.FailingExampleService;
@@ -29,6 +36,9 @@
2936
import org.springframework.context.annotation.Bean;
3037
import org.springframework.context.annotation.Configuration;
3138
import org.springframework.context.annotation.Primary;
39+
import org.springframework.core.Ordered;
40+
import org.springframework.test.util.ReflectionTestUtils;
41+
import org.springframework.util.Assert;
3242

3343
import static org.assertj.core.api.Assertions.assertThat;
3444
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -39,6 +49,7 @@
3949
* @author Phillip Webb
4050
* @author Andy Wilkinson
4151
* @author Andreas Neiser
52+
* @author Madhura Bhave
4253
*/
4354
class MockitoPostProcessorTests {
4455

@@ -123,6 +134,16 @@ void canSpyQualifiedBeanWithPrimaryBeanPresent() {
123134
assertThat(Mockito.mockingDetails(context.getBean("exampleQualified", ExampleService.class)).isSpy()).isTrue();
124135
}
125136

137+
@Test
138+
void postProcessorShouldNotTriggerEarlyInitialization() {
139+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
140+
context.register(FactoryBeanRegisteringPostProcessor.class);
141+
MockitoPostProcessor.register(context);
142+
context.register(TestBeanFactoryPostProcessor.class);
143+
context.register(EagerInitBean.class);
144+
context.refresh();
145+
}
146+
126147
@Configuration(proxyBeanMethods = false)
127148
@MockBean(SomeInterface.class)
128149
static class MockedFactoryBean {
@@ -258,6 +279,14 @@ ExampleService examplePrimary() {
258279

259280
}
260281

282+
@Configuration(proxyBeanMethods = false)
283+
static class EagerInitBean {
284+
285+
@MockBean
286+
private ExampleService service;
287+
288+
}
289+
261290
static class TestFactoryBean implements FactoryBean<Object> {
262291

263292
@Override
@@ -275,6 +304,33 @@ public boolean isSingleton() {
275304
return true;
276305
}
277306

307+
};
308+
309+
static class FactoryBeanRegisteringPostProcessor implements BeanFactoryPostProcessor, Ordered {
310+
311+
@Override
312+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
313+
RootBeanDefinition beanDefinition = new RootBeanDefinition(TestFactoryBean.class);
314+
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition("test", beanDefinition);
315+
}
316+
317+
@Override
318+
public int getOrder() {
319+
return Ordered.HIGHEST_PRECEDENCE;
320+
}
321+
322+
}
323+
324+
static class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
325+
326+
@Override
327+
@SuppressWarnings("unchecked")
328+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
329+
Map<String, BeanWrapper> cache = (Map<String, BeanWrapper>) ReflectionTestUtils.getField(beanFactory,
330+
"factoryBeanInstanceCache");
331+
Assert.isTrue(cache.isEmpty(), "Early initialization of factory bean triggered.");
332+
}
333+
278334
}
279335

280336
interface SomeInterface {

0 commit comments

Comments
 (0)