Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Automatically Inject Mocks into Spring Context

DZone's Guide to

Automatically Inject Mocks into Spring Context

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

When bringing up a Spring context for testing, sometimes following all the dependencies can be a never ending path of bean definitions. In most cases, mocking them out is just fine, and even beneficial if you need to verify() a method on the Mock. We had started with a MocksFactory to inject a mock, but that required a bean definition for every bean being auto wired up. So we came up with a BeanDefinitionRegistryPostProcessor to fill-in all the auto wired beans which we didn't want to waste time defining. Once the definitions have been read in, this bean is given the opportunity to inject some additional beans before the autowiring happens. If an @Autowired field is found, which doesn't have a bean with name of the field, this AutoBeanDeclarer will create a mock for it. This assumes that you're using name matching for Autowiring and not just type matching. It allowed us to remove hundreds of MockFactory bean definitions, without sacrificing the testability of the code.

 

package testing;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;

/**
* Initial idea from: http://javadevelopmentforthemasses.blogspot.com/2008/07/mocking-spring-tests.html
*
* Usage: <bean id="autoConfigurer" class="testing.AutoBeanDeclarer"></bean>
*
* @author jryan
*/
public class AutoBeanDeclarer implements BeanDefinitionRegistryPostProcessor {

private Collection<string> mockedDefinitions;

public AutoBeanDeclarer() {
mockedDefinitions = new ArrayList<string>();
}

private Iterable<field> findAllAutoWired(Class targetBean) {
List<field> declaredFields = Arrays.asList(targetBean.getDeclaredFields());
return Iterables.filter(declaredFields, new Predicate<field>() {
@Override
public boolean apply(Field input) {
return input.isAnnotationPresent(Autowired.class);
}
});
}

private void registerOn(final BeanDefinitionRegistry registry,final String beanName, final Class type){
RootBeanDefinition definition = new RootBeanDefinition(MocksFactory.class);

MutablePropertyValues values = new MutablePropertyValues();
values.addPropertyValue(new PropertyValue("type", type));
definition.setPropertyValues(values);

registry.registerBeanDefinition(beanName, definition);
}

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
for(String beanName: registry.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
String beanClassName = beanDefinition.getBeanClassName();
try {
Class beanClass = Class.forName(beanClassName);
for (final Field field : findAllAutoWired(beanClass)) {
String fieldName = field.getName();
boolean invalidType = field.getType().isArray() || field.getType().isPrimitive();
if( invalidType ) {
continue;
}
if( !registry.isBeanNameInUse(fieldName) ) {
registerOn(registry, fieldName, field.getType());
mockedDefinitions.add(fieldName);
// Now field will be available for autowiring.
}
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(AutoBeanDeclarer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
for(String beanName: mockedDefinitions) {
if( !beanFactory.containsBean(beanName) ) {
Logger.getLogger(AutoBeanDeclarer.class.getName()).log(Level.SEVERE, "Missing definition %s", beanName);
}
}
}
}
</field></field></field></string></string>

 

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}