Skip to content

Testing JdbcTemplates with HSQLDB

I’m writing an application that’s going to perform some serious data munging with a database. The schema has been defined (mostly) but there’s no data I can use to test. For a short period of time I added my own data to the dev db server, but I found that it’s not reliable enough. It was a quick but not so easy solution. So instead, I’ve decided to bite the bullet and use hsqldb for the first time. It seems pretty straightforward but I thought a walk through could be useful. There seems to be some documentation for Hibernate but not for JdbcTemplate. These aren’t strictly unit tests, but rather integration tests.

Here’s what I did, please let me know if you have a better way…

Here’s my Spring config, for what it’s worth…





class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

org.hsqldb.jdbcDriver jdbc:hsqldb:mem:test sa






Created a superclass for my tests. This configures my log4j and spring contexts, as well as reads my SQL script into a StringBuffer:


public class MyTests extends TestCase {

protected static final String CONTEXT_PATH =
"com/my/integration/resource/";
protected static final String CONTEXT_FILE = "applicationContext-MyTests-test.xml";
protected static final String LOG4J_CONFIG = "com/my/resource/MyTests-log4j.xml";
protected static final String SQL_SETUP_FILE="com/my/integration/resource/testdb.sql";
protected ClassPathXmlApplicationContext context;
protected Logger logger;
protected File file;
protected StringBuffer sb;

public MyIntegrationTest() throws Exception {
URL log4JConfigUrl = MyTests.class.getResource("/"
+ LOG4J_CONFIG);
URL dbscriptUrl= MyTests.class.getResource("/" +
SQL_SETUP_FILE);
InputStream in = new FileInputStream( new File(dbscriptUrl.getFile()));
sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line ="";
while( (line = br.readLine()) != null ) {
sb.append(line);
}
DOMConfigurator.configure(log4JConfigUrl);
logger = Logger.getLogger("IntegrationTests");
logger.info("Used log4j configuration from: "
+ LOG4J_CONFIG);
context =
new ClassPathXmlApplicationContext("/" + CONTEXT_PATH + CONTEXT_FILE);
}
}

Here is an example integration test that extends my superclass:


public class MyIntegrationTest extends MyTests {

private ObjectToTest myObj;

public MyIntegrationTest () throws Exception {
myObj = new ObjectToTest ();
myObj.setJdbcTemplate((JdbcTemplate)context.getBean("jdbcTemplate"));
}

protected void setUp() throws Exception {
//Update my Database here
myObj.getJdbcTemplate().update(sb.toString());
}

public void testRunProcess() throws Exception {
myObj.setReportFile(file);
myObj.doStuff();

//assert away!
}
}

Obviously, it seems like a lot of work for such a small example, but given that I’m running a dozen integration tests in numerous classes, the work in the superclass pays off.

Note: I struggled refreshing the database after each test when I ran my tests in Eclipse. Apparently, Eclipse creates a different object for each test, and this can mess you up when you try to refresh since there will only be one instance of the hsqldb. I found the easiest way to avoid this was to add a DROP IF EXISTS... line for each table to the top of the SQL script.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

One Comment

  1. lumpynose wrote:

    I’m a bit of a newbie, working on a photo album in my spare time. This is what I did:
    InitDatabase.java

    DatabaseSetup.java

    ddlUtils

    I haven’t tried unitils but it looks quite nice:

    unitils

    Tuesday, September 11, 2007 at 4:34 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*