Customizing GreenPepper fixture resolution
Prerequisite
To change fixtures resolutions you need to define a custom system under development
Changing how
is finding your Fixtures
This could be useful when you are using for example an IOC, like Spring or just when you want to add locations (packages in java, namespaces in .Net) for
can resolve your fixtures.
Only for specifying location to resolve fixtures (packages in java, namespaces in .Net)
public static class CustomSystemUnderDevelopment extends DefaultSystemUnderDevelopment
{
public CustomSystemUnderDevelopment( String... params )
{
super.addImport("com.mycompany.fixtures");
super.addImport("com.mycompany.specials.fixtures");
}
}
By this custom system under development you tell
to look in "com.mycompany.fixtures" and "com.mycompany.specials.fixtures" to resolve fixtures in specfications that your are running.
Changing Fixture instanciation mechanism (Spring example)
The Spring container system under development
Spring is an Inversion Of Control (IOC)
container get more information about it on Spring website
SpringSystemUnderDevelopment code can be found in greenpepper-extensions-spring: SpringSystemUnderDevelopment.java
package com.greenpepper.extensions.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import com.greenpepper.reflect.DefaultFixture;
import com.greenpepper.reflect.Fixture;
import com.greenpepper.systemunderdevelopment.DefaultSystemUnderDevelopment;
public class SpringSystemUnderDevelopment extends DefaultSystemUnderDevelopment
{
private BeanFactory beanFactory;
public SpringSystemUnderDevelopment(String... applicationCtxes)
{
this.beanFactory = new GreenPepperXMLAplicationContext(applicationCtxes).getBeanFactory();
}
public SpringSystemUnderDevelopment(BeanFactory beanFactory)
{
this.beanFactory = beanFactory;
}
@Override
public Fixture getFixture(String fixtureName, String... params) throws Throwable
{
Fixture fixture;
if (params.length != 0)
{
fixture = super.getFixture(fixtureName, params);
}
else
{
try
{
fixture = new DefaultFixture(beanFactory.getBean(fixtureName));
}
catch (NoSuchBeanDefinitionException e)
{
fixture = new DefaultFixture(instantiateAsAutowiredBean(fixtureName));
}
}
return fixture;
}
private Object instantiateAsAutowiredBean(String fixtureName) throws Exception
{
Class fixtureClass = loadType(fixtureName).getUnderlyingClass();
BeanDefinition beanDef = new RootBeanDefinition(fixtureClass, RootBeanDefinition.AUTOWIRE_AUTODETECT);
DefaultListableBeanFactory fallFactory = new DefaultListableBeanFactory(beanFactory);
fallFactory.registerBeanDefinition(fixtureName, beanDef);
return fallFactory.getBean(fixtureName);
}
}