Skip to content

Commit

Permalink
Merge branch '2.1.x' into 2.2.x
Browse files Browse the repository at this point in the history
Closes gh-19309
  • Loading branch information
wilkinsona committed Dec 5, 2019
2 parents 3c0e7cc + bc53fe0 commit 9b0569c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 130 deletions.
Expand Up @@ -6185,6 +6185,11 @@ We recommend using a `@Bean` method to create and configure the mock in this sit
Additionally, you can use `@SpyBean` to wrap any existing bean with a Mockito `spy`.
See the {spring-boot-test-module-api}/mock/mockito/SpyBean.html[Javadoc] for full details.

NOTE: CGLib proxies, such as those created for scoped beans, declare the proxied methods as `final`.
This stops Mockito from functioning correctly as it cannot mock or spy on `final` methods in its default configuration.
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.
This allows Mockito to mock and spy on `final` methods.

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.

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`.
Expand Down
Expand Up @@ -27,7 +27,6 @@
import java.util.Set;
import java.util.TreeSet;

import org.springframework.aop.scope.ScopedObject;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
Expand Down Expand Up @@ -358,9 +357,6 @@ private void inject(Field field, Object target, String beanName) {
Assert.state(ReflectionUtils.getField(field, target) == null,
() -> "The field " + field + " cannot have an existing value");
Object bean = this.beanFactory.getBean(beanName, field.getType());
if (bean instanceof ScopedObject) {
bean = ((ScopedObject) bean).getTargetObject();
}
ReflectionUtils.setField(field, target, bean);
}
catch (Throwable ex) {
Expand Down Expand Up @@ -425,9 +421,8 @@ private static BeanDefinition getOrAddBeanDefinition(BeanDefinitionRegistry regi
}

/**
* {@link BeanPostProcessor} to handle {@link SpyBean @SpyBean} definitions.
* Registered as a separate processor so that it can be ordered above AOP post
* processors.
* {@link BeanPostProcessor} to handle {@link SpyBean} definitions. Registered as a
* separate processor so that it can be ordered above AOP post processors.
*/
static class SpyPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements PriorityOrdered {

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

@Override
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
return this.mockitoPostProcessor.createSpyIfNecessary(bean, getOriginalBeanNameIfScopedTarget(beanName));
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof FactoryBean || bean instanceof ScopedObject) {
if (bean instanceof FactoryBean) {
return bean;
}
return this.mockitoPostProcessor.createSpyIfNecessary(bean, getOriginalBeanNameIfScopedTarget(beanName));
}

private String getOriginalBeanNameIfScopedTarget(String beanName) {
if (ScopedProxyUtils.isScopedTarget(beanName)) {
return beanName.substring("scopedTarget.".length());
}
return beanName;
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
}

static void register(BeanDefinitionRegistry registry) {
Expand Down

This file was deleted.

0 comments on commit 9b0569c

Please sign in to comment.