
173
database testing (e.g., DbUnit for Java) to insert data directly into the database 
(for “Read” tests) or to verify the post-test contents of the database (for “Cre-
ate/Update/Delete” tests).
A useful trick for keeping our fi xture from becoming persistent during data 
access layer testing is to use Transaction Rollback Teardown (page 668). To 
do so, we rely on the Humble Transaction Controller (see Humble Object on
page 695) DFT pattern when constructing our data access layer. That is, the 
code that reads or writes the database should never commit a transaction; this 
allows the code to be exercised by a test that rolls back the transaction to pre-
vent any of the changes made by the SUT from being applied.
Another way to tear down any changes made to the database during the 
fi xture setup and exercise SUT phases of the test is Table Truncation Tear-
down (page 661). This “brute force” technique for deleting data works only 
when each developer has his or her own Database Sandbox and we want to 
clear out all the data in one or more tables.
Ensuring Developer Independence
Testing the database means we need to have the real database available for 
running these tests. During this testing process, every developer needs to have 
his or her own Database Sandbox. Trying to share a single sandbox among several 
or all developers is a false economy; the developers will simply end up tripping 
over one another and wasting a lot of time.
2
 I have heard many different excuses 
for not giving each developer his or her own sandbox, but frankly none of them 
holds water. The most legitimate concern relates to the cost of a database license 
for each developer—but even this obstacle can be surmounted by choosing 
one of the “virtual sandbox” variations. If the database technology supports it, 
we can use a DB Schema per TestRunner (see Database Sandbox); otherwise, 
we have to use a Database Partitioning Scheme (see Database Sandbox).
Testing with Databases (Again!)
Suppose we have done a good job layering our system and achieved our goal of 
running most of our tests without accessing the real database. Now what kinds 
of tests should we run against the real database? The answer is simple: “As few 
as possible, but no fewer!” In practice, we want to run at least a representative 
sample of our customer tests against the database to ensure that the SUT behaves 
2
 Can you image asking a team of carpenters to share a single hammer?
  Testing with Databases (Again!)