java - Junit/ Spring /hibernate : How to specify an out of container datasource for Hibernate Session in Unit tests

标签 java spring hibernate spring-mvc junit

我有一个 applicationContext.xml,它定义了一个名为“baseDataSource”的 bean

<bean id="baseDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="resourceRef" value="true"/>
    <property name="jndiName" value="java:/MySQLDS20"/>
</bean>

现在通常可以在使用 Spring 和 Hibernate 的 jboss 应用程序中很好地创建它。但是当我尝试通过

实例化这个 IOC 容器作为单元测试的一部分时
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:WEB-INF/applicationContext.xml"})

我收到这个错误:

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:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
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:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

我已经仔细阅读了这里的讨论

http://forum.spring.io/forum/spring-projects/data/7448-problem-running-junit-test-with-jndi-datasource

但似乎没有答案可以解决问题或清楚地解释发生了什么。我的问题是:

如何使这个数据源工作?我不能仅仅为了修改这个单个 bean 而将我的 applicationContext.xml 复制并粘贴到某个 testApplicationContext.xml 中。在不更改此 bean 或复制 xml 配置的情况下,我该怎么做才能在我的 junit 测试中创建容器和 Autowiring (我的 CTO 会开枪打死我)

最佳答案

I cannot copy and paste my applicationContext.xml into some testApplicationContext.xml just for the purpose of modifying this one single bean.

你不必。您只需创建一个仅包含数据源覆盖的配置,然后将两者都加载到您的测试用例中。

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource>
</bean>

然后在您的测试用例中将其与您的实际文件一起加载,bean 定义将覆盖您实际配置中的定义。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:WEB-INF/applicationContext.xml", "applicationContext-test.xml"})

后者应该只包含您想要覆盖/替换的 beans。

关于java - Junit/ Spring /hibernate : How to specify an out of container datasource for Hibernate Session in Unit tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974901/

相关文章:

java - String.indexOf() 函数调用的成本/复杂性是多少

java - 在 Spring 上下文中从外部注入(inject)属性

java - Hibernate(JPA)如何进行急切查询,加载所有子对象

java - JPA合并外键丢失

javascript - 在GWT中用JS显示HTML代码

java - 如何从 openAM 获取 JWT token

java - 属性文件中的 Spring 十六进制字符串

java - 注释指定的 bean 名称与现有的、不兼容的 bean 定义冲突

java - hibernate @OneToMany错误: Duplicate entry '0' for key 'PRIMARY'

java - Swing BoxLayout