Chapter 17. Testing database access

 

Dependency is the key problem in software development at all scales.... Eliminating duplication in programs eliminates dependency.

Kent Beck, Test-Driven Development: By Example

This chapter covers

  • Challenges of database testing
  • Introduction to DbUnit
  • Advanced DbUnit techniques
  • DbUnit best practices

The persistence layer (or, roughly speaking, the database access code) is undoubtedly one of the most important parts of any enterprise project. Despite its importance, the persistence layer is hard to unit test, mainly because of the following issues:

  • Unit tests must exercise code in isolation; the persistence layer requires interaction with an external entity, the database.
  • Unit tests must be easy to write and run; code that accesses the database can be cumbersome.
  • Unit tests must be fast to run; database access is relatively slow.

We call these issues the database unit testing impedance mismatch, in reference to the object-relational impedance mismatch (which describes the difficulties of using a relational database to persist data when an application is written using an object-oriented language).

The database-testing mismatch can be minimized using specialized tools, one of them being DbUnit. In this chapter, we show how DbUnit can be used to test database code, and we not only describe its basic concepts but also present techniques that make its usage more productive and the resulting code easier to maintain.

17.1. The database unit testing impedance mismatch

 
 
 

17.2. Introducing DbUnit

 
 
 
 

17.3. Using datasets to populate the database

 
 
 
 

17.4. Asserting database state with datasets

 
 

17.5. Transforming data using ReplacementDataSet

 

17.6. Creating datasets from existing database data

 

17.7. Advanced techniques

 
 

17.8. Database access testing best practices

 
 

17.9. Summary

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest