java - 如何在注入(inject) JBOSS JNDI 数据源的 Spring 中运行 JUnit 测试

标签 java spring junit jboss jndi

我在 Spring 有一个应用程序,数据库 oracle 在 JBOSS 7.1 上运行。 我想简单地运行一个 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)

如何在不更改 jboss 上下文的情况下在我的测试中注入(inject) JNDI 数据源?

最佳答案

根据this发布,或 this很棒的博文我找到了三种方法来解决我的问题,只需在 JUnitTest 类中创建 BeforeClass 方法即可。

我为社区发布它:

- 解决方案 1
此解决方案需要类路径中的 catalina.jar 和 oracledriver:

@BeforeClass
public static void setUpClass() throws Exception {
    try {
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");            
        InitialContext ic = new InitialContext();

        ic.createSubcontext("jboss");
        ic.createSubcontext("jboss/datasources");
        ic.createSubcontext("jboss/datasources/myDatasource");

        OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
        ds.setURL("jdbc:oracle:thin:@xxxxx:1521:xxxxx");
        ds.setUser("myUserid");
        ds.setPassword("myPass");

        ic.rebind("jboss/datasources/myDatasource", ds);
    } catch (NamingException ex) {
        ex.printStackTrace();
    }
}

如果你使用maven你可以把你的pom:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>catalina</artifactId>
    <version>6.0.37</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.3.0</version>
    <scope>test</scope>
</dependency> 


- 解决方案 2
此解决方案需要在您的类路径中使用 commons-dbcp:

@BeforeClass
public static void setUpDataSource() throws Exception {
    try {
        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
        DriverAdapterCPDS cpds = new DriverAdapterCPDS();
        cpds.setDriver("oracle.jdbc.OracleDriver");
        cpds.setUrl("jdbc:oracle:thin:@xxxxx:1521:xxxxx");
        cpds.setUser("myUsername");
        cpds.setPassword("myPass");

        SharedPoolDataSource dataSource = new SharedPoolDataSource();
        dataSource.setConnectionPoolDataSource(cpds);
        dataSource.setMaxActive(10);
        dataSource.setMaxWait(50);
        builder.bind("jboss/datasources/myDatasource", dataSource);
        builder.activate();
    } catch (NamingException ex) {
        ex.printStackTrace();
    }
}

在你的 pom 中:

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
    <scope>test</scope>
</dependency>


- 解决方案 3
此解决方案使用 Oracle 驱动程序中包含的 OracleConnectionPoolDataSource:

@BeforeClass
public static void setUpDataSource() throws Exception {
    try {
        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();

        OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
        ds.setURL("jdbc:oracle:thin:@xxxxx:1521:xxxxx");
        ds.setUser("myUsername");
        ds.setPassword("myPass");           

        builder.bind("jboss/datasources/myDatasource", ds);
        builder.activate();
    } catch (NamingException ex) {
        ex.printStackTrace();
    }
}

关于java - 如何在注入(inject) JBOSS JNDI 数据源的 Spring 中运行 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19976745/

相关文章:

java - 如何使用Java从Windows Vault中检索用户名和密码?

java - 如何在Android Graph View上创建静态标签?

spring - 条件@PostConstruct注释

java - 在测试中找不到保存的实体

java - 在 ant 中设置 javaagent

Java变量值初始化

spring - 如何在 Spring MVC 中的 MessageConverter 处理之前将响应转换为另一种类型

java - 项目构建良好,但为什么 JUnit 测试失败?

Java 9 + maven + junit : does test code need module-info. java 自己的,放在哪里?

java - Eclipse 4 插件 - 按键绑定(bind)