Mark Needham

Thoughts on Software Development

Testing Hibernate mappings: Setting up test data

with 3 comments

Continuing with my mini Hibernate mappings series, this post talks about the different ways of setting up the test data for our Hibernate tests.

  1. Where to test the mappings from?
  2. How to test for equality?
  3. How to setup the test data?

There are a couple of ways that we can setup data for Hibernate tests.

Insert Hibernate Object

This approach involves creating a new object and saving it to the database using the save method on the Hibernate session.

The benefit of this approach is that our test is pretty clean. It looks like any old unit test where we create an object and then check that what we get back is the same object.

The problem is that we are effectively testing two things – the ability to save and then load our Hibernate object. We will be able to tell whether or not our Hibernate mappings are correct using this approach, but the failures we get when they do fail may not be that obvious – it could just be a database exception which makes the test fail.

SQL Insertion

The other approach I have seen is to write manual JDBC calls to insert data into the various tables in our database and then check that we can load our object from the database using Hibernate.

The advantage of this is that our test is now only testing the mappings when we load the data from the database which helps reduce our test’s invariants or potential points of failure.

The disadvantage is that the tests can seem very brittle – if we make a small change to the column names in our tables then the test setup code may now fail to work anymore.

I’m not really completely happy with either of these approaches – neither seems optimal to me but both can help us achieve our objective. It’s just a matter of choosing which trade off we’ve prepared to accept.

Be Sociable, Share!

Written by Mark Needham

October 30th, 2008 at 11:24 pm

Posted in Hibernate,Testing

Tagged with ,

  • I prefer the approach of using Hibernate to setup test data. Injecting data using sql quickly results in hard to maintain test code, and it won’t tell you when you violate validation logic that happens on the domain object (if you validate before save).

    Using sql to inject data is best used when you need to test some condition that you can can’t get to via hibernate.

    In practice, I’m not too bothered by the fact that you test a save and a load, you can always write test helpers when the application save logic differs from what you want to do.

    In the end, when you are using SQL to inject, you are really testing that the SQL works as well, but that check provides less value since it (probably) isn’t used by the production code.

  • I agree with Kris. I would prefer doing it with Hibernate over SQL. I have experienced severe pain after refactorings on a project where too much was set up with SQL.

  • Pingback: TDD: Driving from the assertion up at Mark Needham()