gpt4 book ai didi

spring - Spring中junit测试如何获取jndi环境变量?

转载 作者:行者123 更新时间:2023-12-01 10:49:10 26 4
gpt4 key购买 nike

我在测试我的应用程序时遇到了 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

感谢您的帮助!

最佳答案

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

使用 JndiObjectFactoryBean 的全部意义在于,您可以使用组件的依赖注入(inject)来将您的应用程序从对 JNDI 的直接依赖中分离出来。

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

现在,如果出于某些非常好的原因您必须直接依赖 JNDI,Spring 确实提供了一种机制来设置内存中的 JNDI 上下文。请参阅 SimpleNamingContextBuilder 的 Javadoc了解详情。

关于spring - Spring中junit测试如何获取jndi环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22533841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com