POJava's JNDIRegistry is useful for simulating the InitialContext normally provided by an application server, but not normally available to your unit tests.
Context ctx = new InitialContext(); ctx.bind("example", "oops"); // The above line throws a NoInitialContextException assertEquals("oops", ctx.lookup("example"));
// Establish a test-friendly context Context newCtx = JNDIRegistry.getInitialContext(); newCtx.bind("example", "alt"); // In the code you're testing, InitialContext now works as designed. Context ctx = new InitialContext(); assertEquals("alt", ctx.lookup("example"));
JNDI is a protocol for Java to access named resources through a managed directory service. One common implementation of JNDI is a registry provided by a J2EE application server, such as Tomcat, to hold system-managed resources such as JDBC DataSources or system global variables. When accessing these resources in your J2EE app, you typically just construct a new instance of InitialContext and use it to lookup values added by your Context. When running your JUnit tests, the InitialContext is not populated, inviting all sorts of workarounds-- many of which leave gaps in your code coverage.
POJava addresses the issue by enabling the unit test environment to create and populate a local JNDI registry. Your unit tests can populate the registry as you expect from your operating environment, allowing you more complete test coverage.