DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • What to Pay Attention to as Automation Upends the Developer Experience
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • Using OpenAI Embeddings Search With SingleStoreDB

Trending

  • What to Pay Attention to as Automation Upends the Developer Experience
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • Using OpenAI Embeddings Search With SingleStoreDB
  1. DZone
  2. Coding
  3. Frameworks
  4. Automatically Inject Mocks into Spring Context

Automatically Inject Mocks into Spring Context

Justin Ryan user avatar by
Justin Ryan
·
Feb. 03, 11 · Interview
Like (1)
Save
Tweet
Share
19.45K Views

Join the DZone community and get the full member experience.

Join For Free
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>

 

Spring Framework

Opinions expressed by DZone contributors are their own.

Trending

  • What to Pay Attention to as Automation Upends the Developer Experience
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • Using OpenAI Embeddings Search With SingleStoreDB

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: