up previous next contents index
Next: Use mock objects Up: Unit Testing Database Code Previous: Two lines of thought   Contents   Index

Create a test database

One line of thought involves creating a test database to use with your JUnit tests. In a professional environment this may mean that you will have the following databases to maintain:

Some people believe that this is too many databases to create/maintain, and it is not worth the effort. Fortunately scripts can be used to alleviate much of this pain, so this point is debatable.

A second concern is that unit tests for database code take much longer to run. This appears to be true, but there are also great ways around this.

Pro's:

  1. You can build real objects from a real database and test them against expected results.
    1. You will catch a lot of routine JDBC/RDBMS coding errors.
  2. While testing against a real database can be slow, with JUnit you don't have to run your database tests all the time. You can run them just once or twice a day, as needed.

Con's:

  1. You may have 3-4 databases to maintain.
  2. It may be difficult to maintain test/development databases.
    1. If this is true then there may be other problems. :)
    2. Scripts can be used to automate database maintenance activities.
    3. This is easily worked around in JUnit.
  3. Tests running against a real database can be slow.
  4. You can't test database exceptions very well.


up previous next contents index
Next: Use mock objects Up: Unit Testing Database Code Previous: Two lines of thought   Contents   Index