Introduction: Driving the Database Layer

Get an introduction to what you’ll learn in this chapter.

TDD for Wordz database layer

In this chapter, we’ll implement a database adapter for one of our ports in the domain model, represented by the WordRepository interface. This will allow our domain model to fetch words to guess from a real database, in this case, using the popular open-source database Postgres. We’ll test-drive both the database setup and the code that accesses the database. To help us do that, we’ll use a test framework that’s designed to simplify writing database integration tests, called DBRider.

By the end of the section, we’ll have written an integration test against a running database, implemented the fetchesWordByNumber() method from the WordRepository interface, and used the Jdbi database access library to help us. We’ll create a database user with permissions on a table storing words to guess. We’ll create that table, and then write a SQL query that Jdbi will use to retrieve the word we’re looking for. We’ll use a named parameter SQL query to avoid some application security issues caused by SQL injections.

Chapter goals

In this chapter, we’re going to cover the foundations of database integration testing with DBRider. Throughout these lessons, we'll explore the basics, drive production code, and build adapters for WordRepository and GameRepository.

Get hands-on with 1400+ tech skills courses.