Jndi使用spring在junit中查找 [英] Jndi lookup in junit using spring
问题描述
如何在junit测试类中查找Jndi名称?我不想为junit使用单独的configration文件而不提供数据源属性?我特别需要在我的测试类中进行jndi查找。
How can I lookup Jndi name in junit test class? I do not want to use separate configration file for junit and not giving datasource properties? I specifically need to do jndi lookup in my test class.
推荐答案
以下是来自官方Spring文档的引用 http://static.springsource .org / spring / docs / 3.1.x / spring-framework-reference / html / testing.html#mock-objects-jndi :
Here is a quote from the official Spring docs http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/testing.html#mock-objects-jndi:
org.springframework.mock.jndi包中包含JNDI SPI的实现,您可以使用它为测试套件或独立应用程序设置简单的JNDI环境。
The org.springframework.mock.jndi package contains an implementation of the JNDI SPI, which you can use to set up a simple JNDI environment for test suites or stand-alone applications.
从这个doc我使用Spring上下文配置类来声明一个数据源:
From this doc I use Spring context configuration class which declares a datasource:
@Configuration
public class JndiDataConfig {
@Bean
public DataSource dataSource() throws Exception {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
}
}
这篇关于Jndi使用spring在junit中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!