Created
January 11, 2013 08:21
-
-
Save jgeraerts/4508903 to your computer and use it in GitHub Desktop.
Load Spring Application with Thucydides JBehave.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.