JUnit testing Hibernate and Spring

Here’s a nice recipe for unit testing Spring configured Hibernate. It allows me to neatly test my Spring configured DAOs and reuse a lot of the Hibernate Session and Transaction configuration beans from my production code. This saves having to rewrite it for my tests and also makes the tests more realistic. I’d rather test my production code rather than use mocks as far as possible.

My App code

I use Spring to create DAOs and configure the datasources, Hibernate session and transaction behaviour. As all of this is just general behaviour rules, I don’t want it cluttering my Java application code.

Briefly, here are the important Spring configured beans:

My datasources (just pulled from JNDI)…

… and my Hibernate properties …

… and my Hibernate session…

… and my DAOs …

… and my transactions …

Very neat. Setting up the Session with the TransactionProxyFactoryBean in this way allows me to avoid all the Session / Transaction boilerplate.  Sessions and Transactions never appear in application code. I just inject the spannerDAO class into my services and call methods.

My Tests

I’d like JUnit to test my DAOs and I’d quite like to configure it in the same way. I need to make a couple of changes though.

First I don’t want to test against the external datasources. That is, I don’t want to depend on an Oracle or SQLServer instance existing somewhere on my local machine or on the network. This just adds a fiddly dependency to every machine capable of building my project. In particular, this requires installing database client software to the ‘clean’ build machine. I’d rather not. Instead, I use an in memory datasource – HyperSQL in this case. This can be run entirely from the JUnit test fixture.

Second, I don’t really want to maintain a test database at all. Whether or not my database is in memory, something needs to create it and load a schema. Fortunately, Hibernate neatly takes care of this for you. Simply set the ‘hbm2ddl.auto’ property to ‘create’. Then it will create the database schema at startup time. That is, when the test fixture is loaded and the HyperSQL database starts up.

As a result, I replace the database connection beans with the following:

Every other bean can be used as is so just leave them be.

All that’s left is to create a test class for the DAOs. We want the test class to see the Spring beans so use the Spring’s AbstractSingleSpringContextTests class, overriding the getConfigLocations method.

Now all that’s left is to get the DAO bean and start making calls to it.

One final point: You could argue that this method of testing is flawed. My production database is SQLServer but I’m testing against HyperSQL. Also, unless I have the setup of the tests create a bucket load of sample data, I’m running my tests against a fresh, empty database. Hardly realistic.

It is true that the above method does not adequately test database functions. However, that is not the aim of these particular tests. They are intended to test the DAO code and Hibernate mappings. To properly test the database functions, we’d use the standard (SQLServer) datasource bean, connected to some dedicated test database. This database would exist within the test environment for the sole purpose of this sort of test. There are a few articles out there on suggested ways to do this, but none of them seem pleasant. Once you start connecting to ‘real’ databases you start getting into all sorts of hassle. You need a database available and maintained alongside your code. You need some way of guaranteeing that the database is ‘consistent’ when you run your tests. So either you reinitialize the database before every test run or you ensure that your test cases always rollback and never commit (which isn’t realistic either). I’m of the opinion that this is not worth the hassle.

But it’s worth keeping in mind the object of the exercise. We are unit testing the DAOs and Hibernate mappings only. We are NOT integration testing the database. So we can be reasonably sure that our DAOs function the way we expect. We can however say nothing about the correctness of our database schema nor anything about what is actually inserted or retrieved from the database. That must be tested seperately.

EDIT: This recipe was expanded upon in my post on DbUnit

One comment

  • Pingback: DbUnit « Don't Panic!

  • Leave a Reply

    Your email address will not be published. Required fields are marked *