Skip to content

Commit 9b0569c

Browse files
committed
Merge branch '2.1.x' into 2.2.x
Closes gh-19309
2 parents 3c0e7cc + bc53fe0 commit 9b0569c

File tree

3 files changed

+10
-130
lines changed

3 files changed

+10
-130
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6185,6 +6185,11 @@ We recommend using a `@Bean` method to create and configure the mock in this sit
61856185
Additionally, you can use `@SpyBean` to wrap any existing bean with a Mockito `spy`.
61866186
See the {spring-boot-test-module-api}/mock/mockito/SpyBean.html[Javadoc] for full details.
61876187

6188+
NOTE: CGLib proxies, such as those created for scoped beans, declare the proxied methods as `final`.
6189+
This stops Mockito from functioning correctly as it cannot mock or spy on `final` methods in its default configuration.
6190+
If you want to mock or spy on such a bean, configure Mockito to use its inline mock maker by adding `org.mockito:mockito-inline` to your application's test dependencies.
6191+
This allows Mockito to mock and spy on `final` methods.
6192+
61886193
NOTE: While Spring's test framework caches application contexts between tests and reuses a context for tests sharing the same configuration, the use of `@MockBean` or `@SpyBean` influences the cache key, which will most likely increase the number of contexts.
61896194

61906195
TIP: If you are using `@SpyBean` to spy on a bean with `@Cacheable` methods that refer to parameters by name, your application must be compiled with `-parameters`.

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Set;
2828
import java.util.TreeSet;
2929

30-
import org.springframework.aop.scope.ScopedObject;
3130
import org.springframework.aop.scope.ScopedProxyUtils;
3231
import org.springframework.beans.BeansException;
3332
import org.springframework.beans.PropertyValues;
@@ -358,9 +357,6 @@ private void inject(Field field, Object target, String beanName) {
358357
Assert.state(ReflectionUtils.getField(field, target) == null,
359358
() -> "The field " + field + " cannot have an existing value");
360359
Object bean = this.beanFactory.getBean(beanName, field.getType());
361-
if (bean instanceof ScopedObject) {
362-
bean = ((ScopedObject) bean).getTargetObject();
363-
}
364360
ReflectionUtils.setField(field, target, bean);
365361
}
366362
catch (Throwable ex) {
@@ -425,9 +421,8 @@ private static BeanDefinition getOrAddBeanDefinition(BeanDefinitionRegistry regi
425421
}
426422

427423
/**
428-
* {@link BeanPostProcessor} to handle {@link SpyBean @SpyBean} definitions.
429-
* Registered as a separate processor so that it can be ordered above AOP post
430-
* processors.
424+
* {@link BeanPostProcessor} to handle {@link SpyBean} definitions. Registered as a
425+
* separate processor so that it can be ordered above AOP post processors.
431426
*/
432427
static class SpyPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements PriorityOrdered {
433428

@@ -446,22 +441,15 @@ public int getOrder() {
446441

447442
@Override
448443
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
449-
return this.mockitoPostProcessor.createSpyIfNecessary(bean, getOriginalBeanNameIfScopedTarget(beanName));
444+
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
450445
}
451446

452447
@Override
453448
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
454-
if (bean instanceof FactoryBean || bean instanceof ScopedObject) {
449+
if (bean instanceof FactoryBean) {
455450
return bean;
456451
}
457-
return this.mockitoPostProcessor.createSpyIfNecessary(bean, getOriginalBeanNameIfScopedTarget(beanName));
458-
}
459-
460-
private String getOriginalBeanNameIfScopedTarget(String beanName) {
461-
if (ScopedProxyUtils.isScopedTarget(beanName)) {
462-
return beanName.substring("scopedTarget.".length());
463-
}
464-
return beanName;
452+
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
465453
}
466454

467455
static void register(BeanDefinitionRegistry registry) {

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

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)