Mark Needham

Thoughts on Software Development

Testing Hibernate mappings: Where to test from?

with 3 comments

I've had the opportunity to work with Hibernate and it's .NET twin NHibernate on several of my projects and one of the more interesting decisions around its use is working out the best way to test the hibernate mappings that hook together our domain model and the database.

There are three decisions to make around how best to do this:

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

This post will focus on the ways I have seen with regards to choosing where to test the mappings from.

Functional Tests

This approach advocates only testing whether we have setup the mappings correctly when we run our acceptance or functional tests – we do not write tests specifically for testing hibernate mappings.

The benefit of this approach is that we are more likely to have acceptance tests in place, so this is just another thing that they can be used to catch.

While this approach is better than not testing at all, from my experiences the test feedback cycle is too slow – it takes too long to change one of the hibernate mappings and then run the test to check if it worked or not.

Repository Tests

With this approach we test whether our hibernate mappings are working as part of our repository tests.

The tricky thing with testing our hibernate mappings this way is that typically we only want to set up one object in the database and then test that Hibernate hydrates it correctly, but our repository doesn't necessarily need a method for finding a single object.

We either end up adding on a method just for testing or we have to try and find our object from a list of other objects and then test it.

On the other hand, this approach does seem to work quite well when we have quite chatty repositories which provide a degree of flexibility around how we can retrieve our objects.

Direct Tests

This approach is my current favourite and involves loading the object directly from the Hibernate session and then testing it.

I was introduced to this idea by a colleague of mine and it seems to fit the idea of testing just one thing more closely than the other two approaches.

The strange thing about this approach is that we are testing directly with an API that is hidden from our system beyond the Repository.

In terms of simplicity with regards to testing hibernate mappings, however, this is the best approach I have seen.

I did a quick survey of some people last week and the most popular way of testing the mappings expressed was using Repository tests.

This post covers the other ways I have seen – are there any others people have come across or are using?

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • HackerNews
  • StumbleUpon
  • Twitter

Written by Mark Needham

October 27th, 2008 at 10:55 pm

Posted in Hibernate,Testing

Tagged with , ,

3 Responses to 'Testing Hibernate mappings: Where to test from?'

Subscribe to comments with RSS or TrackBack to 'Testing Hibernate mappings: Where to test from?'.

  1. [...] started a mini Hibernate series with my last post where I spoke of their being three main areas to think about when it comes to [...]

  2. [...] Where to test the mappings from? [...]

  3. Hi,

    Here where I work we found the same problem and we chose to go with the direct approach. A colleague wrote a small extension to JUnit where he only needs o implement a method that populates an object, and optionally a method that tests two objects of that class for equality. The 'framework' saves the original object to the database (Hypersonic), loads it back, an then calls the method that tests the object for equality.

    Domingos

    1 Nov 08 at 1:20 pm

Leave a Reply