Mock JNDI context that permits String names to be bound to objects. It is intended to simulate JNDI factories in web containers and application servers, and provides only the bare minimum functions. This class contains a static member InitialContextFactory class called Factory that installs a "root" instance of TestJNDIContext as the initial context for the entire JVM. Additional contexts can be bound by supplying pairs of Strings and Objects using {@link #bind(String,Object)}. Objects are looked up and retrieved using {@link #lookup(String)}. All other methods in this class no-op.
For example, simulating a JNDI DataSource requires us to first establish an initial context of java:comp/env
. Inside the initial context, we bind our data source:
Context initCtx = new InitialContext();
initCtx.bind( "java:comp/env", new TestJNDIContext() );
Context ctx = (Context)initCtx.lookup("java:comp/env");
DataSource ds = new TestJDBCDataSource();
ctx.bind( "jdbc/UserDatabase", ds);
@since 2.3