1

我在我的应用程序中使用了 JNDI 连接,它正在工作。但我需要编写 Junits 来测试连接。我们不使用任何弹簧框架。这是我编写的获取 JNDI 连接的方法。

public Connection getConnection() throws SQLException {
        DataSource ds = null;
        InitialContext ic = null;
        Connection con = null;
        try {
            ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:/DBs");
            con = ds.getConnection();
            return con;
        } catch (Exception e) {
            throw new SQLException(e);
        }

}
4

3 回答 3

2

You can make use of the SimpleNamingContextBuilder that comes with the spring-test library. You can use this even if you aren't using Spring as it isn't Spring specific.

Below is an example of setting up a JNDI connection in the @Before of the JUnit test.

package com.example;

import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;


public class SomeTest
{

   @Before
   public void contextSetup () throws Exception
   {
       SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
       DriverManagerDataSource dataSource = new DriverManagerDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:testdb", "sa", "");
       builder.bind("java:comp/env/jdbc/ds1", dataSource);
       builder.bind("java:comp/env/jdbc/ds2", dataSource);
   }

   @Test
   public void testSomething () throws Exception
   {
        /// test with JNDI
   }

}

UPDATE: This solution also uses Spring's DriverManagerDataSource. If you want to use that you will also need the spring-jdbc library. But you don't have to use this, you can create any object you like and put it into the SimpleNamingContextBuilder. For example, a DBCP connection pool, a JavaMail Session, etc.

于 2012-10-05T13:19:16.887 回答
1
OK. After lot of searching i found a solution.And it is working for me. I want to share this to everybody. Hope this thing might help people who are having the same issue. Please add the below code.Add ojdb6.jar and naming-common-4.1.31.jar in your test libraries                                               
 @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("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
            ic.createSubcontext("java:/comp/env/jdbc");

            OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();
            ocpds.setURL("your URL");
            ocpds.setUser("your username");
            ocpds.setPassword("your password");

            ic.bind("java:/yourJNDIName", ocpds);

        } catch (NamingException ex) {
            Logger.getLogger(yourTesTClass.class.getName()).log(Level.SEVERE, null, ex);



        }


    }
于 2012-10-10T18:57:34.083 回答
0

如果它在应用服务器之外运行,那么您可能需要为 InitialContext 的调用提供参数。但也要意识到许多 DataSource 实现是不可序列化的,因此它们无法在容器外工作。

您正在编写的是一个集成测试,它应该在容器中运行。

于 2012-10-05T13:13:09.493 回答