Java Reference
In-Depth Information
database features such as stored procedures. Stored procedures and other non-
portable database design choices make it difficult to unit-test the database in any
way other than against a real instance of the database.
The disadvantage of using a real database instance is that your unit tests can
only run when connected to the network. Alternatively, you could use a local
instance of one of these databases, which means your unit tests would require
additional local environment setup before they could run. Either way, you'll also
be faced with having to rebuild the test data and possibly even the schema
between test suites or even between each unit test. Doing so can be an extremely
slow process even on large enterprise-class database servers. Another problem is
that with a centralized database, multiple developers running the unit tests at the
same time may cause a collision. Therefore, you will have to isolate each devel-
oper with a separate schema. As you can see, the general problem with this
approach is that the unit tests are dependent on a fairly large piece of infrastruc-
ture, which is not ideal to most experienced test-driven developers.
Luckily for Java developers, there is at least one fantastic in-memory database
that makes unit testing of relatively standard database designs quite easy. H SQLDB
is an in-memory database written entirely in Java. It doesn't require any files on disk
or network connectivity to work. Furthermore, it is capable of reproducing most
database designs from typical databases such as Oracle and Microsoft SQL Server.
Even if it can't re-create an entire database due to design complications (such as
stored procedures), it should be able to reproduce most of it. HSQLDB allows you
to rebuild a database, including the schema and test data, very quickly. The suite of
unit tests for i BATIS itself uses HSQLDB and rebuilds the schema and test data
between each individual test. We've personally witnessed test suites of nearly 1,000
database-dependent tests that run in under 30 seconds using HSQLDB .
For more information about HSQLDB , visit http://hsqldb.sourceforge.net/.
Microsoft . NET developers will be happy to know that there are initiatives to port
HSQLDB as well as to create other in-memory database alternatives.
Database scripts
Now that we have the database instance, what do we do about the schema and the
test data? You probably have scripts to create the database schema and to create
the test data. Ideally, you have checked the scripts into your version control system
(such as CVS or Subversion). These scripts should be treated as any other code in
your application. Even if you are not in control of your database, you should
expect regular updates from the people who are. Your application source code
and database scripts should always be in sync, and your unit tests are there to
Search WWH ::




Custom Search