piątek, 25 kwietnia 2008

Simple & fast DB testing

Yes, you're right. This short post is about HSQL - powerful yet surprisingly simple to use, pure-Java RDBMS.
Developing db-related functionalities in Java has never been easier. Using Hibernate's features as on-the-fly schema generation developer can easily stick to changing requirements. But what about testing? Transferring data to and from DB could take considerable amount of time and usage of one, shared, remote db can change test conditions and therefore make results unpredictable.
Solution is straight forward - configure own, isolated DB for all the testing! In Java world there could be only one answer - Hypersonic SQL (HSQL). In simplest example, one could use embedded HSQL running in-memory database.
Only two things are needed to enjoy separated DB test environment:
  • add HSQL to the test class path - using Maven2 it's as simple as adding dependency to project's POM:


  • configure and start org.hsqldb.Server class.
The latter can be done by writing simple wrapping class:

public class HsqlBootstrap
extends Thread{

private Server server;

public HsqlBootstrap() {
server = new Server();

server.setDatabasePath(0, "mem");
server.setDatabaseName(0, "testDb");

public void run() {


public void stopServer() {

There is of course need for configuring Hibernate to use our new, freshly created DB. Adequate properties could be found on various Hibernate-related sites and include using jdbc:hsqldb:mem:testDb connection string ;-)