如何在Spring中使用junit测试获取jndi环境变量?

时间:2014-03-20 13:01:39

标签: spring jndi spring-test

我在测试我的应用程序时遇到了JNDI问题,不是数据源,而是将配置加载到环境中。我的应用程序上下文测试:

<bean class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" id="appDataSource">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="jndiClient" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/client"/>
    <property name="jndiEnvironment">
        <props>
            <prop key="properties1">44</prop>
            <prop key="properties2">0</prop>
            <prop key="properties3">XPTO</prop>
        </props>
    </property>
</bean>

我需要加载这个jndiClient。在我班上:

    @Component
    public class FactoryLocator  {
         @Qualifier("jndiClient")
         @Autowired
         private JndiObjectFactoryBean jndiClient;
运行测试时发生

并发生以下错误:

    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jndi.JndiObjectFactoryBean br.com.simova.bob.factory.FactoryLocator.jndiClient; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jndiClient' defined in class path resource [test-appContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 53 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jndiClient' defined in class path resource [test-appContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:873)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:815)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 55 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    ... 65 more

感谢您的帮助!

1 个答案:

答案 0 :(得分:6)

首先,您的测试通常依赖JNDI存在于测试环境中。

使用JndiObjectFactoryBean的全部意义在于,您可以使用依赖注入组件来将应用程序与对JNDI的直接依赖关系解耦。

考虑到这一点,我强烈建议您永远不会JndiObjectFactoryBean的实例注入到您的某个应用程序组件中。相反,您应该将 JndiObjectFactoryBean返回的对象注入到组件中。通过这样做,您可以在编写单元和集成测试时配置组件的模拟或存根,从而避免使用JNDI进行测试。

现在,如果对于某些非常好的原因,您只需要直接依赖JNDI,Spring确实提供了一种机制来设置内存中的JNDI上下文。有关详细信息,请参阅Javadoc SimpleNamingContextBuilder