1

I have JNDI config for all possible database connectivity in my application. Meanwhile I am using JUNIT to test my applications. I find successful connection form my webservices class invocation but error throws while calling it through junit test class.

My JNDI setting is

public static Connection getConnectionPool() {
    Connection conn = null;
System.out.println("Creating connection pool.");
    try {
        Context initContext = new InitialContext();
        Context envContext  = (Context)initContext.lookup("java:/comp/env");
        DataSource ds = (DataSource)envContext.lookup(getJNDIName());
        conn = ds.getConnection();
    } catch (SQLException e1) {
        e1.printStackTrace();
    } catch (NamingException e) {
        e.printStackTrace();
    }
    return conn;

}

private static String getJNDIName()
{
    if(!readJNDI)
    {
        try{
            Properties prop = new Properties();
            prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
            JNDIName = prop.getProperty("jndi.name");
            System.out.println("JNDIName : "+JNDIName);
            readJNDI = true;
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    return JNDIName;
}

Below mentioned is the error.

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

How do I resolve it.?

Thanks in advance.

1

1 Answer 1

1

You should use a local datasource.

Just pull out the DataSource/TransactionManager definition into a separate XML file, and use that instead of the JNDI one during testing.

You can still use all the rest of your configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.