Prevent beans created with @MockBean from being post-processed

Post-processing of mocked beans causes a number of problems:

 - The mock may be proxied for asynchronous processing which can cause
   problems when configuring expectations on a mock (gh-6573)
 - The mock may be proxied so that its return values can be cached or
   so that its methods can be transactional. This causes problems with
   verification of the expected calls to a mock (gh-6573, gh-5837)
 - If the mock is created from a class that uses field injection, the
   container will attempt to inject values into its fields. This causes
   problems if the mock is being created to avoid the use of one of
   those dependencies (gh-6663)
 - Proxying a mocked bean can lead to a JDK proxy being created
   (if proxyTargetClass=false) as the mock implements a Mockito
   interface. This can then cause injection failures as the types don’t
   match (gh-6405, gh-6665)

All of these problems can be avoided if a mocked bean is not
post-processed. Avoiding post-processing prevents proxies from being
created and autowiring from being performed. This commit avoids
post-processing by registering mocked beans as singletons as well as
via a bean definition. The latter is still used by the context for type
matching purposes.

Closes gh-6573, gh-6663, gh-6664
This commit is contained in:
Andy Wilkinson
2016-08-17 10:19:47 +01:00
committed by Stephane Nicoll
parent 52d7282f5e
commit 0e00a49dcc
12 changed files with 235 additions and 122 deletions

View File

@@ -92,8 +92,7 @@ class DefinitionsParser {
for (ResolvableType typeToMock : typesToMock) {
MockDefinition definition = new MockDefinition(annotation.name(), typeToMock,
annotation.extraInterfaces(), annotation.answer(),
annotation.serializable(), annotation.reset(),
annotation.proxyTargetAware());
annotation.serializable(), annotation.reset());
addDefinition(element, definition, "mock");
}
}

View File

@@ -26,7 +26,6 @@ import java.lang.annotation.Target;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.MockSettings;
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AliasFor;
@@ -140,15 +139,4 @@ public @interface MockBean {
*/
MockReset reset() default MockReset.AFTER;
/**
* Indicates that Mockito methods such as {@link Mockito#verify(Object) verify(mock)}
* should use the {@code target} of AOP advised beans, rather than the proxy itself.
* If set to {@code false} you may need to use the result of
* {@link org.springframework.test.util.AopTestUtils#getUltimateTargetObject(Object)
* AopTestUtils.getUltimateTargetObject(...)} when calling Mockito methods.
* @return {@code true} if the target of AOP advised beans is used or {@code false} if
* the proxy is used directly
*/
boolean proxyTargetAware() default true;
}

View File

@@ -54,13 +54,12 @@ class MockDefinition extends Definition {
}
MockDefinition(ResolvableType typeToMock) {
this(null, typeToMock, null, null, false, null, true);
this(null, typeToMock, null, null, false, null);
}
MockDefinition(String name, ResolvableType typeToMock, Class<?>[] extraInterfaces,
Answers answer, boolean serializable, MockReset reset,
boolean proxyTargetAware) {
super(name, reset, proxyTargetAware);
Answers answer, boolean serializable, MockReset reset) {
super(name, reset, false);
Assert.notNull(typeToMock, "TypeToMock must not be null");
this.typeToMock = typeToMock;
this.extraInterfaces = asClassSet(extraInterfaces);

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2012-2016 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Beans created using Mockito.
*
* @author Andy Wilkinson
*/
public class MockitoBeans implements Iterable<Object> {
private final List<Object> beans = new ArrayList<Object>();
void add(Object bean) {
this.beans.add(bean);
}
@Override
public Iterator<Object> iterator() {
return this.beans.iterator();
}
}

View File

@@ -94,6 +94,8 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
private final BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator();
private final MockitoBeans mockitoBeans = new MockitoBeans();
private Map<Definition, String> beanNameRegistry = new HashMap<Definition, String>();
private Map<Field, RegisteredField> fieldRegistry = new HashMap<Field, RegisteredField>();
@@ -132,6 +134,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
private void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory,
BeanDefinitionRegistry registry) {
beanFactory.registerSingleton(MockitoBeans.class.getName(), this.mockitoBeans);
DefinitionsParser parser = new DefinitionsParser(this.definitions);
for (Class<?> configurationClass : getConfigurationClasses(beanFactory)) {
parser.parse(configurationClass);
@@ -182,7 +185,13 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
String beanName = getBeanName(beanFactory, registry, definition, beanDefinition);
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(1,
beanName);
if (registry.containsBeanDefinition(beanName)) {
registry.removeBeanDefinition(beanName);
}
registry.registerBeanDefinition(beanName, beanDefinition);
Object mock = createMock(definition, beanName);
beanFactory.registerSingleton(beanName, mock);
this.mockitoBeans.add(mock);
this.beanNameRegistry.put(definition, beanName);
if (field != null) {
this.fieldRegistry.put(field, new RegisteredField(definition, beanName));

View File

@@ -22,6 +22,7 @@ import java.util.Set;
import org.mockito.Mockito;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
@@ -70,6 +71,17 @@ public class ResetMocksTestExecutionListener extends AbstractTestExecutionListen
}
}
}
try {
MockitoBeans mockedBeans = beanFactory.getBean(MockitoBeans.class);
for (Object mockedBean : mockedBeans) {
if (reset.equals(MockReset.get(mockedBean))) {
Mockito.reset(mockedBean);
}
}
}
catch (NoSuchBeanDefinitionException ex) {
// Continue
}
if (applicationContext.getParent() != null) {
resetMocks(applicationContext.getParent(), reset);
}