在jUnit中设置JNDI数据源

Joe*_*Joe 16 java junit jndi

我正在尝试设置一些jUnit测试.我们的数据库由服务器使用JNDI连接.我们有一个xml描述了root.xml中的设置.如何设置jUnit以连接数据库?我宁愿让它只读取root.xml中的内容,但我仍然愿意设置它,无论如何都有效.

Flo*_*oFu 14

我找到了这个博客:https: //blogs.oracle.com/randystuph/entry/injecting_jndi_datasources_for_junit

关于H2数据源:http: //www.h2database.com/javadoc/org/h2/jdbcx/JdbcConnectionPool.html

所以我的代码:

package com.example.test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.h2.jdbcx.JdbcConnectionPool;

import junit.framework.TestCase;

public class JunitDataSource extends TestCase {

    public void setUp() throws Exception {
        // rcarver - setup the jndi context and the datasource
        try {
            // Create initial context
            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("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
            ic.createSubcontext("java:/comp/env/jdbc");

            JdbcConnectionPool ds = JdbcConnectionPool.create(
                    "jdbc:h2:file:src/main/resources/test.db;FILE_LOCK=NO;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE", "sa", "sasasa");
            // Construct DataSource
            // OracleConnectionPoolDataSource ds = new
            // OracleConnectionPoolDataSource();
            // ds.setURL("jdbc:oracle:thin:@host:port:db");
            // ds.setUser("MY_USER_NAME");
            // ds.setPassword("MY_USER_PASSWORD");

            ic.bind("java:/mydatasourcename", ds);
        } catch (NamingException ex) {
            Logger.getLogger(JunitDataSource.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void testSimple() throws Exception {

        // Obtain our environment naming context
        Context initCtx = new InitialContext();

        // Look up our datasource
        DataSource ds = (DataSource) initCtx.lookup("java:/mydatasourcename");

        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();

        ResultSet rset = stmt.executeQuery("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");


        while (rset.next()) {
          System.out.println("<<<\t"+rset.getString("TABLE_NAME"));
        }


    }

}
Run Code Online (Sandbox Code Playgroud)

注意:我必须在Tomcat的bin目录中添加Tomcat Library和jar才能使其正常工作


Joe*_*Joe 9

我发现最好的方法是使用名为Simple-Jndi的东西.

我把它添加到maven文件中:

    <dependency>
        <groupId>simple-jndi</groupId>
        <artifactId>simple-jndi</artifactId>
        <version>0.11.4.1</version>
        <scope>test</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

您可以在此下载该软件包,下载包含说明手册. http://code.google.com/p/osjava/downloads/detail?name=simple-jndi-0.11.4.1.zip&can=2&q=

添加到项目后,您只需按照说明添加几个属性文件.

但是,在添加依赖项之后,我相信您可以以编程方式添加jndi资源,而不是使用属性文件.你做这样的事情:( new InitialContext()).rebind("datasource",myDatasource);