19 Testing database applications

 

This chapter covers

  • Examining the challenges of database testing
  • Implementing tests for JDBC, Spring JDBC, Hibernate, and Spring Hibernate applications
  • Comparing the different approaches to building and testing database applications

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

--Kent Beck, Test-Driven Development: By Example

The persistence layer (or, roughly speaking, 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 due to the following three 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). We’ll discuss the issues in more detail in this chapter and show possible implementation and testing alternatives for Java database applications.

19.1 The database unit testing impedance mismatch

19.1.1 Unit tests must exercise code in isolation

19.1.2 Unit tests must be easy to write and run

19.1.3 Unit tests must be fast to run

19.2 Testing a JDBC application

19.3 Testing a Spring JDBC application

19.4 Testing a Hibernate application

19.5 Testing a Spring Hibernate application

19.6 Comparing the approaches for testing database applications

Summary