Home » Java » Enterprise Java » Create JNDI resources for JUnit testing using Spring

About Christian Grobmeier

Christian is a passionated software developer, architect and trainer. He is a member and VP of the Apache Software Foundation, working on projects like Struts, log4j and others. He founded Time & Bill and constantly tries out new ideas.

Create JNDI resources for JUnit testing using Spring

Until recently, I had static methods setting up my in memory database (HSQLDB). I called these methods in setUp/tearDown of my JUnit tests. This felt always a bit unnatural to me as I use Spring and everything should run through it’s application context.


Creating a simple JNDI bean

As I use JNDI in production, I had to created a JNDI resource for my application as well. A simple Spring Bean using Apache Commons DBCP does the trick:

public class JndiBean {

    public JndiBean() {
        try {
            DriverAdapterCPDS cpds = new DriverAdapterCPDS();

            SharedPoolDataSource dataSource = new SharedPoolDataSource();

            SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
            builder.bind("java:comp/env/jdbc/timeandbill", dataSource);
        } catch (NamingException | ClassNotFoundException ex) {

You should not use this basic configuration without thinking twice, but it works well for unit testing.

First, I created a driver adapter, containing whatever I need for connecting to my database. It could be MySQL, Postgres or whatever else you prefer.

Then I create a SharedPoolDatasource. It is not really necessary to keep up a lot of connections, as tests usually run sequential. Even if not, the Spring context usually will be created at least per test class. It is unlikely you get out a benefit of pooling here, but I wanted to stick to what production servers usually do.

The SimpleNamingContextBuilder is what eventually bind the perviously created data source to the JNDI context. As you see, it’s a straightforward thing todo: just bind it, then activate and you are done.

Adding the JNDI bean to the context

The next step is to add this to a second applicationContext.xml, which is loaded by JUnit tests only. I resides in my Unit-tests folder and contains:

<bean id="jndi" class="de.grobmeier.tab.webapp.JndiBean" lazy-init="false" />

These annotations on my tests make sure I load all applicationContext files:

public class TimeConverterTest {

My production applicationContext contains this:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/timeandbill"/>
    <property name="resourceRef" value="true" />

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

As there is no Java EE server running at JUnit level. Instead the JNDI connection was created manually. In production, the JUnits applicationContext is not loaded and the Java EE container provides the JNDI resource.

Side note: I found Mybatis relies on Springs autowire “byType” feature.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments