Monday, May 25, 2009

Spring 2.5 - Unit testing with JNDI

My problem was that I had a java.util.Properties object with a bunch of properties in JNDI but I also needed to be able to unit test the classes that were using these properties. I was using Spring so that made things easier.

Being a big fan of Unit Testing I was determined not to let a little thing like JNDI stop me from getting all green on my JUnit test results.

My first step was to separate out some of my applicationContext.xml logic into 2 files. One for the "real" application that will run under GlassFish - let's call it my-jndi.xml. And the other for running unit tests - let's call it my-jndi-test.xml.

my-jndi.xml had the following:







Then in my web.xml I had the following:



contextConfigLocation

/WEB-INF/my-jndi.xml
/WEB-INF/my-application.xml




my-jndi-test.xml had the following:



class="org.springframework.beans.factory.config.PropertiesFactoryBean">


classpath:test.properties


true



Then in my base test class I had the following:


protected String[] getConfigLocations() {
return new String[] {
"file:web/WEB-INF/nexus-jndi-test.xml",
"file:web/WEB-INF/nexus-application.xml"
};

No comments:

Post a Comment