Unit-testing mit JNDI-Abhängigkeiten
Passiert ja gerne mal, dass man irgendwelche Java-Projekte mit JNDI-Kram Unit-testen will und dann Klimmzüge machen muss. Mit Spring kann man das ein bißchen leichter haben:
public class SpringWithJNDIRunner extends SpringJUnit4ClassRunner {
public static boolean isJNDIactive;
/**
* JNDI is activated with this constructor.
*
* @param klass
* @throws InitializationError
* @throws NamingException
* @throws IllegalStateException
*/
public SpringWithJNDIRunner(Class<?> klass) throws InitializationError,
IllegalStateException, NamingException {
super(klass);
synchronized (SpringWithJNDIRunner.class) {
if (!isJNDIactive) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"test-datasource.xml");
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind("java:/my-ds",
applicationContext.getBean("dataSource"));
builder.activate();
isJNDIactive = true;
}
}
}
}
Gesehen bei Integration Testing for Spring Applications with JNDI Connection Pools. Danke an Manu PK!