1

I used JNDI connection in my application and it is working. But I need to write Junits to test the connection. We dont use any spring framework. This is the method i wrote to get JNDI connection.

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);
        }

}
Vishwa
  • 117
  • 1
  • 2
  • 12
  • I used Simple-JNDI to achieve this. See here: http://stackoverflow.com/questions/6838191/simple-jndi-contextfactory/41659886#41659886 – Holger Thurow Jan 23 '17 at 16:38
  • I am using Simple-JNDI to achieve this. See here: http://stackoverflow.com/questions/6838191/simple-jndi-contextfactory/41659886#41659886 – Holger Thurow Jan 23 '17 at 16:49

3 Answers3

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.

Robert Hanson
  • 579
  • 2
  • 7
  • How to handle this org.spring.jdbc.CannotGetJdbcConnectionException. I am throwing Exeption, but it is saying required throwable but found org.spring.jdbc.CannotGetJdbcConnectionException – Vishwa Oct 05 '12 at 14:36
  • I posted an update, does the update help? I forgot to mention that I am using DriverManagerDataSource, which is part of spring-jdbc. You can use that if you want, or you can replace that with any other DataSource class, like the one you are currently using in your webapp. – Robert Hanson Oct 05 '12 at 14:53
  • Thanks for ur reply. But is there any other way. The problem is i am not able to connect to database in my junit. Is there any other solution to this problem – Vishwa Oct 05 '12 at 17:34
  • Like I said, the object you put into the SimpleNameContextBuilder can be anything. In my tests I use an in-memory DB for testing. But if that isn't a workable solution you could always create a mock object that implements DataSource and puts it into the context. It's just a container, what you put in there is up to you. – Robert Hanson Oct 05 '12 at 17:52
  • This also requires the tomcat libraries to supply the jndi factory. I used org.apache.tomcatcatalina6.0.44test – Andrew Russell Feb 03 '16 at 03:07
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);



        }


    }
Vishwa
  • 117
  • 1
  • 2
  • 12
0

If this is running outside the app server, then you'll likely need to supply parameters to the call for the InitialContext. But also realize that many DataSource implementations are not serializable so they won't work outside the container.

What you're writing is an integration test and it should be run in the container.

tdrury
  • 2,307
  • 1
  • 22
  • 25
  • ya your correct!. oracle-ds.xml is in container and my getconnection method is in a jar which is used in webapplication. So any other suggestion to test this connection! – Vishwa Oct 05 '12 at 13:34