Mocking an In-memory JNDI DataSource

Learn to configure and inject an in-memory DataSource instance into JUnit tests outside any Java EE container. This is a suggested solution to test or use classes that depend on container’s (for example Tomcat) JNDI environment.

In-memory DataSource instance helps in decoupling the tests from any kind of external dependency.

1. Maven Dependency

We will be using the Simple-JNDI library’s JNDI implementation which is entirely memory-based. No server instance is started. Download its latest version from Maven repository.


2. Configuring JNDI Context and DataSources

To setup the initial context used by the JNDI, we need to place the file in the application’s classpath. As we are using this feature for unit testing, we can place the file in 'src/test/resources' directory.
  • java.naming.factory.initial is a part of the JNDI specification and specifies the context factory class used to create the initial context. It is a mandatory property.
  • is another mandatory property and points to the directory location where we store the files used to define the context objects.
  • controls if all InitialContext objects will share the same memory.
  • – Its value is prepended to every value loaded into the system. Thus simulates the JNDI environment of Tomcat.

Now we need to create one or multiple instances of a DataSource. So, create a property file in the directory location as configured in

Simple-JNDI will load all the property files in this location. We have created one such file, Here we are creating a DataSource for H2 database.


3. Initializing DataSource in Unit Tests

To initialize a DataSource instance, start with creating an InitialContext object and lookup the Context using java:/comp/env (as specified in ‘’ value).

Using the Context, we can lookup datasources as configured in And finally, we can use the DataSource instance further in the unit tests.

public class TestMockDataSource {

  private static InitialContext initContext;

  public static void setup() throws Exception {
    initContext = new InitialContext();

  public void whenMockJndiDataSource_thenReturnJndiDataSource() throws Exception {
    Context envContext = (Context) this.initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource) envContext.lookup("datasource/ds");

        "org.h2.Driver::::jdbc:h2:mem:testdb::::sa", ds.toString());

    //Use the DataSource as needed

    Connection conn = ds.getConnection();

  public static void tearDown() throws Exception {

Do not forget to close the datasources at the end.

4. Conclusion

In this short tutorial, we learned to create and inject an in-memory DataSource instance that can be used to mock J2EE container provided JNDI DataSource instance.

Happy Learning !!

Sourcecode on Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.