我是Spring的新手,想知道如何创建使用模拟数据源的JUnit测试以及如何使用JNDI上下文?目前,我的应用程序使用tomcat中的JNDI上下文来检索连接,并通过该连接从数据库中检索数据.所以我想我需要模拟JNDI调用和数据检索.关于解决这个问题的最佳方法的任何好的指针都会很棒!非常感谢!
我在春天有一个应用程序,在JBOSS 7.1上运行数据库oracle.我想测试我的服务层bean只是运行junit测试.在我的spring上下文中,我使用了像这样的jndi数据源:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jboss/datasources/myDatasource" />
<property name="resourceRef" value="true"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
当我运行加载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)
Run Code Online (Sandbox Code Playgroud)
如何在我的测试中注入JNDI数据源而不更改jboss中的上下文?