DBUnit - Unit Test your Database Schema

DBUnit is a JUnit extension which can be used to load a database with a sample of data and then execute tests directly against the database. This can be useful for verifying that you did not mess up existing functionality of the database when adding new features to a schema. It can also enable Test Driven Development for the database functionality.

I am writing this tutorial to help increase the 'prior art' available for folks to draw upon. I needed database unit testing functionality and DBUnit fit the bill. But I had not used Java in a while so had a lot of gaps to fill in when trying to make examples work. Hopefully this will save you some time during evaulation or implementation.

Eventually this will encompass setup and a few complete examples you can compile and run yourself. I also want to catalog a few common useful

Pros:

  • Cross Platform : Java and JDBC Support
  • Can reset database to known point. Good for isolating tests on their own and to avoid cascading house of cards when 1 test early goes wrong and future dependencies are broken.
  • Unit Tests can be a learning tool for development. If you have a complex construct and a way to test it, then you can point developers at it as an example of how to use it.
  • A way to support continuous integration, support for tests to be driven by Ant, Maven, or plain old Java classes

Cons:

  • Written in Java
  • May not scale well for large datasets (think unit testing of functionality, not performance testing).
  • Environment setup can be involved.