I’m a fairly recent convert to unit testing, but still when I’m under pressure to get something finished, I skip right to integration tests. Despite the guilt I still use the junit framework for these integration tests… I know the unit testing purists will be rolling their eyes at this point, but this is something I’ve found useful to do.
A while back I wrote about using hsqldb in unit/integration tests running in memory. The process works well because you can effectively avoid having to clean up the database before running a test, and you don’t have to worry about closing the connections in the tearDown() method either. Just kill the process and the database goes away. Provide a setup sql script and load that before running your test, and you’re golden.
But for more involved integration tests, it’s nice to be able to see the data that’s been modified. Or write a test that performs a first phase of data manipulation, followed by a second test that relies on the output of the first. This can be accomplished by an in-memory database, but it’s more satisfying for me to actually see what occurred after the test has run. I’m probably just not detail-oriented enough to set up a test thoroughly while I’m doing development, and I like being able to exam the database results visually for that added level of comfort.
Hsqldb provides some really nice configuration options for running the database as a server:
- There’s Standard Standalone server.
- You can also run the standalone server as an in-memory server
- There’s also a webserver server mode that makes use of http
- As of 1.7.2 hsqldb provides the ability to run more than one database in multiple server modes. (ie simultaneous access to multiple in-process and memory-only databases)
Read on to see how I set this up for my tests:
In my tests, I need to read from one db and load into another. The problem is that the database server I could use is in a remote office over a crappy network. So there’s a strong motivation for me to run my tests locally. I needed to set up two databases, and as I said earlier, I want to be able to see the results of my work. So (this is the part where the ruby on rails people laugh):
- Configure two databases for my server
- Configure my database explorer client
- Configure my app to work with hsqldb
Configure two databases for my server
To configure this in Eclipse, you’ll need to create a Java App to run. Run->Run, Java Application, right-click New. Main Class will be org.hsqldb.Server. I use a separate eclipse project to store my jars, so I selected that as my project. Next add the hsqldb.jar to the classpath. You can also specify arguments, but I opted to use a server.properties file instead. You can apply or save the app now.
Since I specified my jar project for the app I created, I created a file, server.properties in the base directory of that project. The file looks like this:
#this is the default so I may not actually need this server.port=9001 #mounts a file-based (persistent) database with alias 'clinicalconcept' #database connection url would be 'jdbc:hsqldb:hsql://localhost/clinicalconcept' #but '/clinicalconcept' can be omitted because database.0 is the default server.database.0=file:C:/eceppda/work/schemadb server.dbname.0=clinicalconcept #mounts a file-based (persistent) database with the alias 'staged' #datbase connection url would be 'jdbc:hsqldb:hsql://localhost/staged' server.database.1=file:C:/eceppda/work/stagedb server.dbname.1=staged #Display messages in console server.silent=false
There are of course more options you can view them here.
Configure my database explorer client
I use MyEclipse. I like it a lot. It has a database explorer plugin, among other things. So I use this database explorer. I had to specify that I was connecting to a hsqldb, and provide it with the hsqldb jar. For connection strings, I used these (one for each database):
- jdbc:hsqldb:hsql://localhost/clinicalconcept
- jdbc:hsqldb:hsql://localhost/staged
Configure my app to work with hsqldb
Lastly, in the app I’m writing I created a properties file to hold the database information:
# staging database db.driver=org.hsqldb.jdbcDriver db.url=jdbc:hsqldb:hsql://localhost/clinicalconcept db.username=sa db.password= stage.driver=org.hsqldb.jdbcDriver stage.url=jdbc:hsqldb:file:C:/eceppda/stagedb/stagedb stage.username=sa stage.password=
Then in my spring config I specify the propertyconfigurator and my datasources:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>com/my/shite/resource/updater.properties</value>
</list>
</property>
</bean>
<bean id="dbSourceDS" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>
<bean id="stageSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${stage.driver}</value>
</property>
<property name="url">
<value>${stage.url}</value>
</property>
<property name="username">
<value>${stage.username}</value>
</property>
<property name="password">
<value>${stage.password}</value>
</property>
</bean>
And that should do it. Run the app you configured in spring, you should see more verbose output that will say that it’s read the server.properties file. Refer to my previous post on loading data into the db’s for your tests…
One Comment
Hello, how reference two tables in two schemas?
Post a Comment