我在春季有一个在 JBOSS 7.1 上运行数据库 oracle 的应用程序。我想简单地运行一个 junit 测试来测试我的服务层 bean。在我的 spring 上下文中,我使用这样的 jndi 数据源:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jboss/datasources/myDatasource" />
<property name="resourceRef" value="true"/>
</bean>
当我运行加载 spring 上下文测试的 junit 测试时,我收到如下异常:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [context.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:1455)
如何在我的测试中注入 JNDI 数据源而不改变我在 jboss 中的上下文?