Chapter 6. Test-driving data access
To begin with, the chapter explains why data-access code deserves special test-driven treatment: it crosses architectural boundaries, depends on third-party APIs, and is often tested manually even though that is slow and error-prone. The author frames the problem in terms of designing for testability, especially by separating persistence concerns from business logic with the DAO pattern. A DAO keeps domain objects ignorant of storage details, making it easier to swap JDBC, Spring, Hibernate, or even file-based persistence without changing the application code.
The chapter then contrasts unit tests and integration tests for persistence code. For pure JDBC, test-driving can be cumbersome because the API requires lots of plumbing, so the examples rely on test doubles for DataSource, Connection, PreparedStatement, and ResultSet. Spring’s JdbcTemplate reduces that burden by hiding boilerplate behind a template method style API and a RowMapper, making tests smaller and the production code cleaner. Hibernate goes a step further by mapping objects to tables automatically, allowing DAO tests to focus on interactions with SessionFactory, Session, and Query, while also showing how to test exception handling and reduce duplication with fuzzy matching and named queries.
After unit testing, the chapter moves to integration tests to verify that the code really works against a real database schema, not just against mocked expectations. It discusses choosing a lightweight database such as an in-memory HSQLDB instance, creating schemas automatically or with scripts, and keeping tests fast by using transactional fixtures that roll back changes after each test. It also covers ways to populate test data, either through Hibernate or with DbUnit and XML datasets, and closes by comparing unit and integration tests as complementary tools rather than rivals. Finally, it briefly applies the same design principles to file-system access, recommending interfaces, streams, custom file facades, and dedicated temp directories to keep file-related code testable.
A pile of logical layers of a system. If we want to test-drive the persistence logic layer, which of the underlying layers should our tests exercise?
Data Access Object pattern: Domain objects being created, retrieved, and persisted through a Data Access Object interface without knowledge of the underlying database technology
Listing 6.2. Test for a pure JDBC DAO implementation
Collaboration between the template code provided by the Spring Framework and the DAO implementation provided by the developer
DAO implementation extends from Spring’s JdbcDaoSupport class that provides JdbcTemplate functionality
Listing 6.4. Test for the RowMapper implementation for Person objects
Listing 6.6. Full test for the JdbcTemplate-based PersonDao implementation
Listing 6.7. JdbcTemplate-based PersonDao implementation
The SessionFactory, Session, and Query interfaces represent the main concepts of the Hibernate API.
Listing 6.10. Full test for finding people by their last name
Listing 6.11. Hibernate-based implementation of the PersonDao
Listing 6.12. Testing that the session is closed even after an exception
Listing 6.14. Skeleton for our first integration test class
Listing 6.15. Configuring a SessionFactory for tests
Example directory structure with a set of delta change scripts for creating the database schema incrementally. The four scripts in the directory src/db/deltas will be applied in order according to the numerical prefix.
Listing 6.17. Example delta script with an @UNDO-block for rollback
Listing 6.18. Skeleton for our first integration test class
Listing 6.19. Integration test rolls back changes using a transactional fixture
Listing 6.20. Integration test populating data using the Hibernate API
Listing 6.21. Abstract base class for DbUnit-based integration tests
Listing 6.22. Concrete test class extending the base class from listing 6.21
Listing 6.24. Using DbUnit for comparing data sets
Test Driven ebook for free