Merge branch 'gh-29639' into 2.6.x
Closes gh-29909
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 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,6 +26,7 @@ import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.aop.scope.ScopedProxyUtils;
|
||||
import org.springframework.beans.BeansException;
|
||||
@@ -433,6 +434,8 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
|
||||
|
||||
private static final String BEAN_NAME = SpyPostProcessor.class.getName();
|
||||
|
||||
private final Map<String, Object> earlySpyReferences = new ConcurrentHashMap<>(16);
|
||||
|
||||
private final MockitoPostProcessor mockitoPostProcessor;
|
||||
|
||||
SpyPostProcessor(MockitoPostProcessor mockitoPostProcessor) {
|
||||
@@ -446,6 +449,10 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
|
||||
|
||||
@Override
|
||||
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
|
||||
if (bean instanceof FactoryBean) {
|
||||
return bean;
|
||||
}
|
||||
this.earlySpyReferences.put(getCacheKey(bean, beanName), bean);
|
||||
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
|
||||
}
|
||||
|
||||
@@ -454,7 +461,14 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
|
||||
if (bean instanceof FactoryBean) {
|
||||
return bean;
|
||||
}
|
||||
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
|
||||
if (this.earlySpyReferences.remove(getCacheKey(bean, beanName)) != bean) {
|
||||
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
private String getCacheKey(Object bean, String beanName) {
|
||||
return StringUtils.hasLength(beanName) ? beanName : bean.getClass().getName();
|
||||
}
|
||||
|
||||
static void register(BeanDefinitionRegistry registry) {
|
||||
|
||||
Reference in New Issue
Block a user