Skip to content

Instantly share code, notes, and snippets.

@jgeraerts
Created January 11, 2013 08:21
Show Gist options
  • Save jgeraerts/4508903 to your computer and use it in GitHub Desktop.
Save jgeraerts/4508903 to your computer and use it in GitHub Desktop.
Load Spring Application with Thucydides JBehave.
public class SpringThucydidesJUnitStories extends ThucydidesJUnitStories {
@Override
public InjectableStepsFactory stepsFactory() {
return SpringAutowiringThucydidesStepFactory.withStepsFromPackage(getRootPackage(), configuration()).andClassLoader(getClassLoader());
}
public static class SpringAutowiringThucydidesStepFactory extends ThucydidesStepFactory {
private static volatile ConfigurableApplicationContext ctx;
private final static Object lock = new Object();
private static void loadCtx() {
synchronized (lock) {
if (ctx == null) {
ctx = new ClassPathXmlApplicationContext("/spring-context.xml");
ctx.registerShutdownHook();
}
}
}
public SpringAutowiringThucydidesStepFactory(Configuration configuration, String rootPackage, ClassLoader classLoader) {
super(configuration, rootPackage, classLoader);
loadCtx();
}
@Override
public Object createInstanceOfType(Class<?> type) {
Object o = super.createInstanceOfType(type);
ctx.getBeanFactory().autowireBeanProperties(
o, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
return o;
}
public static ThucydidesStepFactory withStepsFromPackage(String rootPackage, Configuration configuration) {
return new SpringAutowiringThucydidesStepFactory(configuration, rootPackage, defaultClassLoader());
}
private static ClassLoader defaultClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
}
}
@Lexijok
Copy link

Lexijok commented Jul 17, 2014

Thanks for this!
I noticed it only allows for @autowire within the JBehave Steps in the src/test/java classes and not within the Thucydides Steps in src/main/java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment