liveBook · Manning

Chapter 6: Chapter 6. Test-driving data access

What you will learn in chapter 6 of Test Driven: Practical TDD and Acceptance TDD for Java Developers

The introduction motivates test-driving data access by highlighting the importance of persistent data, the shortcomings of manual inspection, and the gap left by faking persistence in business-logic tests.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction motivates test-driving data access by highlighting the importance of persistent data, the shortcomings of manual inspection, and the gap left by faking persistence in business-logic tests.

Key terms: persistent data, data-access code, visual inspection, test doubles, business logic, persistence services, persistence layer

6.1 Exploring the problem domain

This section explains why data-access code presents distinctive testing challenges.

Key terms: data-access code, layers, third-party APIs

6.1.1 Data access crosses boundaries

This section maps the layers involved in data access and introduces the testing choices created by their boundaries.

Key terms: application logic, persistence logic, persistence framework, JDBC API, JDBC driver, relational database, unit test, coupling, stub, persistence logic layer interface

6.1.2 Separating layers with the DAO pattern

This section explains how the DAO pattern separates application code from persistence technology and how persistence libraries reduce data-access plumbing.

Key terms: Data Access Object, DAO, persistent domain object, PersonDao, persistence technology, persistence frameworks, Hibernate, JdbcTemplate, persistence logic, plumbing

6.2 Driving data access with unit tests

This section frames unit-testing data access by identifying the maintenance cost of scattered JDBC code and explaining why unit tests should avoid real external dependencies.

Key terms: JDBC, data-access code, unit tests, developer tests, test double, database

6.2.1 Witnessing the tyranny of the JDBC API

This section test-drives a pure JDBC DAO with test doubles to illustrate the complexity of testing JDBC code and the need to verify resource cleanup.

Key terms: JDBC, plumbing code, checked exceptions, resources, PersonDao, findByLastname, DataSource, Connection, PreparedStatement, ResultSet

6.2.2 Reducing pain with Spring’s JdbcTemplate

This section shows how Spring’s JdbcTemplate simplifies JDBC code and how to test-drive a DAO and its row mapper using mocks and EasyMock argument matchers.

Key terms: JdbcTemplate, JdbcDaoSupport, boilerplate, RowMapper, ResultSet, PersonRowMapper, findByLastname, query, EasyMock, eq

6.2.3 Closer to test-driven nirvana with Hibernate

This section explains Hibernate’s main abstractions and demonstrates test-driving a DAO with mocks, including exception handling and the limits of mocked query tests.

Key terms: object-relational mapping, Hibernate, persistent fields, transient, RowMapper, SessionFactory, Session, Query, HQL, SQL

6.3 Writing integration tests before the code

This section explains why data-access unit tests with mocks cannot replace checking the code and persistence framework against a real database.

Key terms: data-access objects, test doubles, mock objects, expectations, persistence framework, integration tests

6.3.1 What is an integration test?

This section explains data-access integration tests, the risks they catch beyond unit tests, and why a faster test database can differ from production.

Key terms: integration tests, persistence logic, persistence framework, relational database, database schema, domain model, query statements, object-relational mappings, mock objects, production environment

6.3.2 Selecting the database

This section weighs production compatibility, team setup, and debugging access when selecting a database for integration tests.

Key terms: JDBC, reserved words, UNIQUE constraints, production database, persistence layer, unique identifiers, sequence generator, primary-key generators, configuration management system, automated build scripts

6.4 Integration tests in action

This section introduces a Hibernate integration-testing example, from persisting an object to maintaining test data with transactional fixtures.

6.4.1 Writing our first Hibernate integration test

This section sketches a Hibernate DAO integration test and configures it to run against an in-memory database.

Key terms: integration tests, unit tests, test doubles, HibernatePersonDao, Person, SessionFactory, transient, ID, Session, transaction

6.4.2 Creating the database schema

This section compares ways to version, create, and incrementally change a database schema for integration testing and deployment.

Key terms: database schema, DDL, SQL scripts, version control, Hibernate, SessionFactory, mapping documents, hibernate.hbm2ddl.auto=create-drop, sql-type, incremental DDL scripts

6.4.3 Implementing the production code

This section implements HibernatePersonDao to persist a Person using a SessionFactory and a transaction.

Key terms: HibernatePersonDao, SessionFactory, Session, transaction, save(person)

6.4.4 Staying clean with transactional fixtures

This section explains how transactional fixtures preserve database state between integration tests, along with their performance benefit and limitation.

Key terms: transactional fixture, fixture, Transaction, Session, setUp, tearDown, transactional fixtures, known state, test database, transactional behavior

6.5 Populating data for integration tests

The section explains why integration tests of database reads need test data and notes the limitations of populating it with SQL scripts.

Key terms: integration test, read operation, persistence framework, SQL scripts, INSERT statements, portable

6.5.1 Populating objects with Hibernate

This section shows how to populate integration-test data with Hibernate and explains the approach’s benefits and limitations.

Key terms: Hibernate API, object graphs, flushing the session, findByLastname, test data, test logic, external SQL file, object-relational mappings, persistence framework, raw SQL

6.5.2 Populating data with DbUnit

This section shows how to use DbUnit to populate a known database state from external data and compare database contents with expected data.

Key terms: DatabaseTestCase, DatabaseConnection, DbUnitIntegrationTestCase, data set, flat XML, dataset, table, column, IDataSet, Assertion

6.6 Should I drive with unit or integration tests?

This section contrasts test-first development with traditional testing stages and introduces unit and integration tests as ways to drive implementation.

Key terms: test-driven development, unit tests, test doubles, integration tests

6.6.1 TDD cycle with integration tests

This section describes the integration-test TDD cycle, including DbUnit’s extra data and schema steps and the tools needed to keep the cycle practical.

Key terms: DbUnit, data files, database schema, green bar, integration tests, unit test–based TDD, green states, database refactorings, plumbing, persistence framework

6.6.2 Best of both worlds

This section weighs integration tests against unit tests for data access and recommends using both according to the task.

Key terms: integration tests, unit tests, data-access code, test doubles, integration test, domain objects, golden middle ground

6.7 File-system access

This section explains why unit tests should minimize file-system access and use stream abstractions to supply file data.

Key terms: unit test, file system, speed, maintainability, streams, java.io.Writer, java.io.Reader, file system access

6.7.1 A tale from the trenches

This section illustrates how a storage interface makes a background content downloader easier to unit-test without frequent file-system access.

Key terms: ContentStorage, ContentDownload, unit tests, file I/O

6.7.2 Practices for testable file access

This section presents design and test-fixture practices that make file-access functionality easier to test.

Key terms: file access, custom interface, stream objects, file handles, FileInputStream, custom file interface, façade, metadata, Jakarta Commons VFS, virtual file system

6.8 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Chapter 5. Test-driving web components Chapter 7: Chapter 7. Test-driving the unpredictable →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.