Merge branch 'gh-11077'

pull/14206/merge
Phillip Webb 6 years ago
commit 64be6ae564

@ -19,6 +19,7 @@ package org.springframework.boot.test.mock.mockito;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ -85,14 +86,14 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
.getQualifiedAttributeName(ConfigurationClassPostProcessor.class,
"configurationClass");
private static final BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator();
private final Set<Definition> definitions;
private ClassLoader classLoader;
private BeanFactory beanFactory;
private final BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator();
private final MockitoBeans mockitoBeans = new MockitoBeans();
private Map<Definition, String> beanNameRegistry = new HashMap<>();
@ -183,13 +184,13 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
RootBeanDefinition beanDefinition = createBeanDefinition(definition);
String beanName = getBeanName(beanFactory, registry, definition, beanDefinition);
String transformedBeanName = BeanFactoryUtils.transformedBeanName(beanName);
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(1,
beanName);
if (registry.containsBeanDefinition(transformedBeanName)) {
BeanDefinition existing = registry.getBeanDefinition(transformedBeanName);
copyBeanDefinitionDetails(existing, beanDefinition);
registry.removeBeanDefinition(transformedBeanName);
}
registry.registerBeanDefinition(transformedBeanName, beanDefinition);
Object mock = createMock(definition, beanName);
Object mock = definition.createMock(beanName + " bean");
beanFactory.registerSingleton(transformedBeanName, mock);
this.mockitoBeans.add(mock);
this.beanNameRegistry.put(definition, beanName);
@ -202,62 +203,58 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
RootBeanDefinition definition = new RootBeanDefinition(
mockDefinition.getTypeToMock().resolve());
definition.setTargetType(mockDefinition.getTypeToMock());
definition.setFactoryBeanName(BEAN_NAME);
definition.setFactoryMethodName("createMock");
definition.getConstructorArgumentValues().addIndexedArgumentValue(0,
mockDefinition);
if (mockDefinition.getQualifier() != null) {
mockDefinition.getQualifier().applyTo(definition);
}
return definition;
}
/**
* Factory method used by defined beans to actually create the mock.
* @param mockDefinition the mock definition
* @param name the bean name
* @return the mock instance
*/
protected final Object createMock(MockDefinition mockDefinition, String name) {
return mockDefinition.createMock(name + " bean");
}
private String getBeanName(ConfigurableListableBeanFactory beanFactory,
BeanDefinitionRegistry registry, MockDefinition mockDefinition,
RootBeanDefinition beanDefinition) {
if (StringUtils.hasLength(mockDefinition.getName())) {
return mockDefinition.getName();
}
Set<String> existingBeans = findCandidateBeans(beanFactory, mockDefinition);
Set<String> existingBeans = getExistingBeans(beanFactory,
mockDefinition.getTypeToMock(), mockDefinition.getQualifier());
if (existingBeans.isEmpty()) {
return this.beanNameGenerator.generateBeanName(beanDefinition, registry);
return MockitoPostProcessor.beanNameGenerator.generateBeanName(beanDefinition,
registry);
}
if (existingBeans.size() == 1) {
return existingBeans.iterator().next();
}
String primaryCandidate = determinePrimaryCandidate(registry, existingBeans,
mockDefinition.getTypeToMock());
if (primaryCandidate != null) {
return primaryCandidate;
}
throw new IllegalStateException(
"Unable to register mock bean " + mockDefinition.getTypeToMock()
+ " expected a single matching bean to replace but found "
+ existingBeans);
}
private void copyBeanDefinitionDetails(BeanDefinition from, RootBeanDefinition to) {
to.setPrimary(from.isPrimary());
}
private void registerSpy(ConfigurableListableBeanFactory beanFactory,
BeanDefinitionRegistry registry, SpyDefinition definition, Field field) {
String[] existingBeans = getExistingBeans(beanFactory, definition.getTypeToSpy());
BeanDefinitionRegistry registry, SpyDefinition spyDefinition, Field field) {
Set<String> existingBeans = getExistingBeans(beanFactory,
spyDefinition.getTypeToSpy(), spyDefinition.getQualifier());
if (ObjectUtils.isEmpty(existingBeans)) {
createSpy(registry, definition, field);
createSpy(registry, spyDefinition, field);
}
else {
registerSpies(registry, definition, field, existingBeans);
registerSpies(registry, spyDefinition, field, existingBeans);
}
}
private Set<String> findCandidateBeans(ConfigurableListableBeanFactory beanFactory,
MockDefinition mockDefinition) {
QualifierDefinition qualifier = mockDefinition.getQualifier();
private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory,
ResolvableType type, QualifierDefinition qualifier) {
Set<String> candidates = new TreeSet<>();
for (String candidate : getExistingBeans(beanFactory,
mockDefinition.getTypeToMock())) {
for (String candidate : getExistingBeans(beanFactory, type)) {
if (qualifier == null || qualifier.matches(beanFactory, candidate)) {
candidates.add(candidate);
}
@ -265,21 +262,20 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
return candidates;
}
private String[] getExistingBeans(ConfigurableListableBeanFactory beanFactory,
private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory,
ResolvableType type) {
Set<String> beans = new LinkedHashSet<>(
Arrays.asList(beanFactory.getBeanNamesForType(type)));
String resolvedTypeName = type.resolve(Object.class).getName();
String typeName = type.resolve(Object.class).getName();
for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class)) {
beanName = BeanFactoryUtils.transformedBeanName(beanName);
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
if (resolvedTypeName
.equals(beanDefinition.getAttribute(FACTORY_BEAN_OBJECT_TYPE))) {
if (typeName.equals(beanDefinition.getAttribute(FACTORY_BEAN_OBJECT_TYPE))) {
beans.add(beanName);
}
}
beans.removeIf(this::isScopedTarget);
return StringUtils.toStringArray(beans);
return beans;
}
private boolean isScopedTarget(String beanName) {
@ -291,49 +287,49 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
}
}
private void createSpy(BeanDefinitionRegistry registry, SpyDefinition definition,
private void createSpy(BeanDefinitionRegistry registry, SpyDefinition spyDefinition,
Field field) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(
definition.getTypeToSpy().resolve());
String beanName = this.beanNameGenerator.generateBeanName(beanDefinition,
registry);
spyDefinition.getTypeToSpy().resolve());
String beanName = MockitoPostProcessor.beanNameGenerator
.generateBeanName(beanDefinition, registry);
registry.registerBeanDefinition(beanName, beanDefinition);
registerSpy(definition, field, beanName);
registerSpy(spyDefinition, field, beanName);
}
private void registerSpies(BeanDefinitionRegistry registry, SpyDefinition definition,
Field field, String[] existingBeans) {
private void registerSpies(BeanDefinitionRegistry registry,
SpyDefinition spyDefinition, Field field, Collection<String> existingBeans) {
try {
registerSpy(definition, field,
determineBeanName(existingBeans, definition, registry));
String beanName = determineBeanName(existingBeans, spyDefinition, registry);
registerSpy(spyDefinition, field, beanName);
}
catch (RuntimeException ex) {
throw new IllegalStateException(
"Unable to register spy bean " + definition.getTypeToSpy(), ex);
"Unable to register spy bean " + spyDefinition.getTypeToSpy(), ex);
}
}
private String determineBeanName(String[] existingBeans, SpyDefinition definition,
BeanDefinitionRegistry registry) {
private String determineBeanName(Collection<String> existingBeans,
SpyDefinition definition, BeanDefinitionRegistry registry) {
if (StringUtils.hasText(definition.getName())) {
return definition.getName();
}
if (existingBeans.length == 1) {
return existingBeans[0];
if (existingBeans.size() == 1) {
return existingBeans.iterator().next();
}
return determinePrimaryCandidate(registry, existingBeans,
definition.getTypeToSpy());
}
private String determinePrimaryCandidate(BeanDefinitionRegistry registry,
String[] candidateBeanNames, ResolvableType type) {
Collection<String> candidateBeanNames, ResolvableType type) {
String primaryBeanName = null;
for (String candidateBeanName : candidateBeanNames) {
BeanDefinition beanDefinition = registry.getBeanDefinition(candidateBeanName);
if (beanDefinition.isPrimary()) {
if (primaryBeanName != null) {
throw new NoUniqueBeanDefinitionException(type.resolve(),
candidateBeanNames.length,
candidateBeanNames.size(),
"more than one 'primary' bean found among candidates: "
+ Arrays.asList(candidateBeanNames));
}
@ -351,7 +347,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
}
}
protected Object createSpyIfNecessary(Object bean, String beanName)
protected final Object createSpyIfNecessary(Object bean, String beanName)
throws BeansException {
SpyDefinition definition = this.spies.get(beanName);
if (definition != null) {
@ -480,7 +476,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
@Override
public Object getEarlyBeanReference(Object bean, String beanName)
throws BeansException {
return createSpyIfNecessary(bean, beanName);
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
}
@Override
@ -489,10 +485,6 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
if (bean instanceof FactoryBean) {
return bean;
}
return createSpyIfNecessary(bean, beanName);
}
private Object createSpyIfNecessary(Object bean, String beanName) {
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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,9 +26,11 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.test.mock.mockito.example.ExampleService;
import org.springframework.boot.test.mock.mockito.example.FailingExampleService;
import org.springframework.boot.test.mock.mockito.example.RealExampleService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import static org.assertj.core.api.Assertions.assertThat;
@ -84,6 +86,79 @@ public class MockitoPostProcessorTests {
.isTrue();
}
@Test
public void canMockPrimaryBean() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
context.register(MockPrimaryBean.class);
context.refresh();
assertThat(Mockito.mockingDetails(context.getBean(MockPrimaryBean.class).mock)
.isMock()).isTrue();
assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isMock())
.isTrue();
assertThat(Mockito
.mockingDetails(context.getBean("examplePrimary", ExampleService.class))
.isMock()).isTrue();
assertThat(Mockito
.mockingDetails(context.getBean("exampleQualified", ExampleService.class))
.isMock()).isFalse();
}
@Test
public void canMockQualifiedBeanWithPrimaryBeanPresent() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
context.register(MockQualifiedBean.class);
context.refresh();
assertThat(Mockito.mockingDetails(context.getBean(MockQualifiedBean.class).mock)
.isMock()).isTrue();
assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isMock())
.isFalse();
assertThat(Mockito
.mockingDetails(context.getBean("examplePrimary", ExampleService.class))
.isMock()).isFalse();
assertThat(Mockito
.mockingDetails(context.getBean("exampleQualified", ExampleService.class))
.isMock()).isTrue();
}
@Test
public void canSpyPrimaryBean() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
context.register(SpyPrimaryBean.class);
context.refresh();
assertThat(
Mockito.mockingDetails(context.getBean(SpyPrimaryBean.class).spy).isSpy())
.isTrue();
assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isSpy())
.isTrue();
assertThat(Mockito
.mockingDetails(context.getBean("examplePrimary", ExampleService.class))
.isSpy()).isTrue();
assertThat(Mockito
.mockingDetails(context.getBean("exampleQualified", ExampleService.class))
.isSpy()).isFalse();
}
@Test
public void canSpyQualifiedBeanWithPrimaryBeanPresent() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
context.register(SpyQualifiedBean.class);
context.refresh();
assertThat(Mockito.mockingDetails(context.getBean(SpyQualifiedBean.class).spy)
.isSpy()).isTrue();
assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isSpy())
.isFalse();
assertThat(Mockito
.mockingDetails(context.getBean("examplePrimary", ExampleService.class))
.isSpy()).isFalse();
assertThat(Mockito
.mockingDetails(context.getBean("exampleQualified", ExampleService.class))
.isSpy()).isTrue();
}
@Configuration
@MockBean(SomeInterface.class)
static class MockedFactoryBean {
@ -137,6 +212,88 @@ public class MockitoPostProcessorTests {
}
@Configuration
static class MockPrimaryBean {
@MockBean(ExampleService.class)
private ExampleService mock;
@Bean
@Qualifier("test")
public ExampleService exampleQualified() {
return new RealExampleService("qualified");
}
@Bean
@Primary
public ExampleService examplePrimary() {
return new RealExampleService("primary");
}
}
@Configuration
static class MockQualifiedBean {
@MockBean(ExampleService.class)
@Qualifier("test")
private ExampleService mock;
@Bean
@Qualifier("test")
public ExampleService exampleQualified() {
return new RealExampleService("qualified");
}
@Bean
@Primary
public ExampleService examplePrimary() {
return new RealExampleService("primary");
}
}
@Configuration
static class SpyPrimaryBean {
@SpyBean(ExampleService.class)
private ExampleService spy;
@Bean
@Qualifier("test")
public ExampleService exampleQualified() {
return new RealExampleService("qualified");
}
@Bean
@Primary
public ExampleService examplePrimary() {
return new RealExampleService("primary");
}
}
@Configuration
static class SpyQualifiedBean {
@SpyBean(ExampleService.class)
@Qualifier("test")
private ExampleService spy;
@Bean
@Qualifier("test")
public ExampleService exampleQualified() {
return new RealExampleService("qualified");
}
@Bean
@Primary
public ExampleService examplePrimary() {
return new RealExampleService("primary");
}
}
static class TestFactoryBean implements FactoryBean<Object> {
@Override

@ -0,0 +1,89 @@
/*
* Copyright 2012-2018 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 org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.example.CustomQualifier;
import org.springframework.boot.test.mock.mockito.example.CustomQualifierExampleService;
import org.springframework.boot.test.mock.mockito.example.ExampleService;
import org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.RealExampleService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
/**
* Test {@link SpyBean} on a test class field can be used to replace existing bean while
* preserving qualifiers.
*
* @author Andreas Neiser
*/
@RunWith(SpringRunner.class)
public class SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests {
@SpyBean
@CustomQualifier
private ExampleService service;
@Autowired
private ExampleServiceCaller caller;
@Autowired
private ApplicationContext applicationContext;
@Test
public void testMocking() throws Exception {
this.caller.sayGreeting();
verify(this.service).greeting();
}
@Test
public void onlyQualifiedBeanIsReplaced() {
assertThat(this.applicationContext.getBean("service")).isSameAs(this.service);
ExampleService anotherService = this.applicationContext.getBean("anotherService",
ExampleService.class);
assertThat(anotherService.greeting()).isEqualTo("Another");
}
@Configuration
static class TestConfig {
@Bean
public CustomQualifierExampleService service() {
return new CustomQualifierExampleService();
}
@Bean
public ExampleService anotherService() {
return new RealExampleService("Another");
}
@Bean
public ExampleServiceCaller controller(@CustomQualifier ExampleService service) {
return new ExampleServiceCaller(service);
}
}
}
Loading…
Cancel
Save