0

I am using WebSphere Liberty 8.5.5 server and writing some batch test cases on this server. I am trying to mock jndi lookup object for testing but while setting up dataSource object in test case I am getting below error, Does anyone know how to set INITIAL_CONTEXT_FACTORY

ERROR : javax.naming.NoInitialContextException: Need to specify class name in environment or system prop erty, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344) at javax.naming.InitialContext.lookup(InitialContext.java:411)

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
Jay
  • 42
  • 11

1 Answers1

1

You need to set the class name of your initial context factory in the environment of the InitialContext constructor.

For example:

Hashtable<Object, Object> env = new Hashtable<>();
env.put(InitialContext.INITIAL_CONTEXT_FACTORY, "com.example.MockICF");
new InitialContext(env);

Where com.example.MockICF might be written as:

public class MockICF implements InitialContextFactory {

    private static Context ctx = new Mockery().mock(Context.class);

    @Override
    public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
        return ctx;
    }
}
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61