Friday, February 4, 2011

Unit test framework with Liquibase+HsqlDb+Spring transactions

When looking at options for testing DAO classes I came up with a framework using the following

1. Liquibase - For maintaing data/schema scripts

2. HSQLDB - Inmemory database for test cases

3. Spring transactions - For maintaining clean state between two test cases which make DB updates i .e all data changes made by the test cases will be rolled back by the end of the test case.

I am using spring beans to initialize all the above. I will explain each of them in detail.

1. Initializing the HSQLDB

location="classpath*" />

<bean id="hsqlDataSource" class="org.apache.commons.dbcp.BasicDataSource"
<property name="driverClassName" value="${database.driverClassName}">
<property name="url" value="${database.url}">
<property name="username" value="${database.username}">
<!-- <property name="password" value="${jdbc.password}"> -->

All the values will be picked up from the file mentioned in the classpath.

2. Setting up Liquibase

I am using the following method to initialize Liquibase

public class TestFramework {

private DataSource dataSource;

public void initializeLiquiBase(String changeLogFile) throws Exception {
Connection conn = dataSource.getConnection();
Liquibase liquibase = new Liquibase(changeLogFile,
new ClassLoaderResourceAccessor(), new HsqlConnection(conn));
// Uncomment the following line when testing with mysql database
* Liquibase liquibase = new Liquibase(changeLogFile, new
* ClassLoaderResourceAccessor(), new JdbcConnection(conn));


public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;


Corresponding spring configuration.

<bean id="testFramework" class="com.mycompany.framework.unittest.TestFramework">
<property name="dataSource" ref="hsqlDataSource">

The changeLogFile will be provided by the user of the frame work , which i will explain shortly.

3. Setting up Spring transactions.

<bean id="transactionManager"
<property name="dataSource" ref="hsqlDataSource">

<tx:annotation-driven manager="transactionManager">

These are the only classes/configuration present in my framework

The maven dependencies required are as follows


Now let me explain how the framework will be used by the classes testing the framework

A typical test class for DAO will be as follows

@ContextConfiguration(locations = {
"classpath*:/META-INF/spring/test-spring-jdbc-module-context.xml" })
public class TestAccountDAOSpringJdbc {

private TestFramework testFramework;

private AccountDAOSpringJdbc accountDao;

public void setUpBeforeClass() throws Exception {
System.out.println("Setup before class called");

public void testSaveUpdate() throws Exception {
// Create a new Account
Account account = accountDao.get(1);



The important thing to note here are

1. Initializing the framework beans - which contains HSQLDB and spring transaction initialization.

2. Initializing the test framework with liquibase scripts

This ensure that the database tables are created and test data is inserted before test cases run.


3. Using @Transactional annotation before test cases which updates the Database . This ensures that test cases do not commit anything into DB and all test cases can be independent of each other.

With some infrastructure setup like this , writing test cases for DAO become very easy.

No comments:

Free Domain Names @!