September 2003
[ vmassol ] 08:32, Saturday, 27 September 2003

Let's imagine you wish to perform unit test for code that calls the database. Let's also imagine that you want to test in integration, i.e. verify that the SQL query does actually goes to the database and returns database data.

The traditional approach is to:

  1. Preload static data before the test suite runs
  2. Load specific test related data in the database before each test. A variation is to restore the modified data at the end of the test

There are several disadvantages to point 2:

  • Finding out the specific data that needs to be set up for the test is time-consuming and you often need to know the full database schema (and not only the domain you're working on). This is especially true for big projects
  • There are referential integrity concerns that forces you to set up again data for a lot of other tables that the ones you are concerned with as they are linked through keys

One solution that our project team discussed yesterday is about using transaction rollbacks. It would work as follows:

  • Before the test suite starts, load the database with full data (this is done only once and usually takes a good 5 minutes for complex projets)
  • Before the test starts, start a transaction (in JUnit's setUp() method for example)
  • run the test
  • rollback the transaction to restore the database data in a pristine state

I was suprised to see no mention of this on the articles mentioning database unit testing (http://www.dallaway.com/acad/dbunit.html, http://www.dbunit.org/bestpractices.html). The reason may be that there are some glitches with this technique that make it impossible to use in practice... I'd like to know what you think? Have you done this before?

Notes:

  • The code under tests should not create a new transaction as nested transactions are usually not supported
[ vmassol ] 19:05, Sunday, 21 September 2003

I have just released version 2.0 of the StatCvs plugin for Maven.

The Maven StatCvs plugin is a plugin for StatCvs-XML that generates CVS statistic reports.

Changes in this version:

  • Migrated the plugin to use the new statcvs-xml jar (which generates XML xdoc output instead of HTML).

You can download the StatCvs Maven 2.0 plugin here.