> Markdown version of [/videos/839-three-and-a-half-ways-of-testing-your-repositories?t=3234](https://www.wearedevelopers.com/videos/839-three-and-a-half-ways-of-testing-your-repositories?t=3234). Every page supports `.md` or `Accept: text/markdown`. Links point to the HTML versions so they work for humans too. Agent guide: [/agents.md](https://www.wearedevelopers.com/agents.md). --- # Three-and-a-half Ways of Testing your Repositories Directly mocking database clients creates brittle tests that mask crucial API errors. Discover how to use ephemeral containers and HTTP stubs to achieve true environmental parity. - **Speakers:** Daniel Raniz Raneland - **Event:** WeAreDevelopers LIVE - **Published:** December 15, 2023 - **Duration:** 59:20 - **URL:** https://www.wearedevelopers.com/videos/839-three-and-a-half-ways-of-testing-your-repositories ## Summary Testing repository implementations within the infrastructure layer of a domain-driven design architecture often presents significant challenges. Evaluating the interaction between applications and remote backends like PostgreSQL, Amazon S3, or DynamoDB requires careful consideration to avoid brittle or overly complex test suites. Through a test-driven development (TDD) approach, evaluating distinct testing strategies illustrates the trade-offs between execution speed, maintenance overhead, and environmental parity. The initial strategy of directly mocking client libraries offers lightning-fast execution and simple setup, but relies heavily on implementation details. This tight coupling inevitably results in high maintenance overhead and risks masking crucial API errors, especially in dynamic programming languages. Alternatively, intercepting network requests to mock the service via HTTP stubbing libraries allows the actual client library code to execute. Validating realistic API interactions this way ensures that tests survive client library upgrades and remain decoupled from internal package mechanics, though it requires slightly more verbose request configuration. For rigid service dependencies like SQL databases, asserting on specific database queries produces highly brittle tests that fail upon minor code refactoring. While testing against shared persistent databases offers high environmental parity, it introduces constant resource contention and complex automated cleanup challenges. To optimize stability without sacrificing production-like guarantees, developers can adopt temporary containerized databases—such as via Testcontainers. Spinning up ephemeral backend containers allows for true roundtrip testing, where a system can effectively "store" and then "retrieve" data sequentially. This validates the ultimate repository behavior strictly through abstractions rather than implementation specifics, massively reducing test fragility while maintaining zero cleanup residue. **Keywords:** domain-driven design architecture, repository pattern implementation, test-driven development workflows, client library mocking, HTTP REST intercepting, AWS S3 API testing, Amazon DynamoDB mocks, PostgreSQL testcontainers integration, ephemeral database containers, automated test fixture cleanup, roundtrip data testing, test environment parity, SQL query debugging, HTTP API mock servers, infrastructure layer validation ## Chapters 1. **Speaker introduction and software engineering background** (00:02) — A brief overview of the speaker's software engineering journey and consulting experience. 1. **Role of repositories in domain-driven design architectures** (01:42) — Defining bounded contexts and placing the repository pattern within a multilayered software architecture. 1. **Structuring repository tests using behavior-driven software syntax** (04:18) — Applying test-driven development and behavior-driven syntax to test repository interactions with external APIs. 1. **Implementing backend client mocks for repository tests** (07:07) — Replacing an external API dependency with a dynamic mock object to verify expected interactions. 1. **Advantages and drawbacks of mocking backend clients** (13:14) — How mocking provides fast test execution but increases complexity and hides integration errors. 1. **Mocking backend services for REST API integration** (16:34) — Intercepting client network calls locally to validate database responses without external connections. 1. **Evaluating the strategy of mocking backend services** (21:58) — How service mocking surfaces real client edge cases while increasing startup time and test verbosity. 1. **Challenges of mocking relational database client libraries** (24:50) — The brittleness of mocking sequential database cursors and exact parameterized SQL queries. 1. **Executing test queries against true database replications** (27:57) — Connecting tests to a persistent development database to avoid writing cumbersome parameter mocks. 1. **Evaluating the drawbacks of managing shared test databases** (30:31) — Analyzing how shared database instances mimic production environments but introduce unreliable cleanup workflows. 1. **Deploying temporary test containers for database operations** (33:44) — Bootstrapping an ephemeral database runtime to ensure isolated query testing without cleanup failures. 1. **Evaluating test isolation using temporary container services** (38:20) — Why local container setups require runtime dependencies and slightly impact unit test duration. 1. **Decoupling tests via implementation agnostic roundtrip testing** (39:19) — Verifying database insertions primarily through data retrieval queries to avoid relying on explicit table structures. 1. **Summary of repository testing strategies and trade-offs** (40:48) — Reviewing core differences between mocks, actual database environments, and ephemeral container instances. 1. **Exploring best practices for selecting testing mocking frameworks** (45:28) — Evaluating external mock environments and handling licensed database interactions for efficient build pipelines. 1. **Balancing isolated unit and integration tests for repositories** (48:59) — Employing unit tests for individual functions and integration endpoints for complete structural verification. 1. **Maintaining repository tests in automated deployment pipelines** (51:33) — Handling database migrations and container cleanup loops effectively inside continuous integration workflows. 1. **Managing backward compatibility and automated database migrations** (53:54) — Utilizing migration frameworks alongside local tests to enforce snapshot consistencies across repository versions. ## Related Moments - [Core architectural assumptions for expressive and scalable testing](https://www.wearedevelopers.com/videos/100215-smart-test-automation-the-experience-of-legacy-transformation) (from "SMART Test Automation - the experience of legacy transformation") - [Validating database interactions in local integration tests](https://www.wearedevelopers.com/videos/812-building-reliable-serverless-applications-with-aws-cdk-and-testing) (from "Building Reliable Serverless Applications with AWS CDK and Testing") - [Reviewing real-world configurations and handling untested repositories](https://www.wearedevelopers.com/videos/100289-automate-or-stagnate-keep-your-dependencies-up-to-date-with-renovate) (from "Automate or Stagnate: Keep Your Dependencies up to date with Renovate") - [Audience Q&A on CDK implementation and automated testing](https://www.wearedevelopers.com/videos/740-the-power-of-cloud-development-kit-cdk-how-to-get-the-most-out-of-it) (from "The power of Cloud Development Kit (CDK): How to get the most out of it") - [Defining software stacks and robust execution testing mechanisms](https://www.wearedevelopers.com/videos/19-building-high-performance-and-scalable-architectures-for-enterprises) (from "Building high performance and scalable architectures for enterprises") - [Mocking external dependencies in unit testing layers](https://www.wearedevelopers.com/videos/402-one-click-to-production-test-and-automate-your-application) (from "One-click-to-production: Test and automate your application") ## Related Articles - [Liuba Gonta and Yuliya Khadasevic - GitHub Copilot Beyond the Basics - 10 Ways to Elevate Your Coding](https://www.wearedevelopers.com/magazine/490-liuba-gonta-and-yuliya-khadasevic-github-copilot-beyond-the-basics-10-ways-to-elevate-your-coding) - [Building AI Solutions with Rust and Docker](https://www.wearedevelopers.com/magazine/494-building-ai-solutions-with-rust-and-docker) - [Integration And E2E Testing: Are They Complementary or Interchangeable?](https://www.wearedevelopers.com/magazine/86-integration-and-e2e-testing-are-they-complementary-or-interchangeable) - [Making Data Warehouses Fast: A Developer’s Story](https://www.wearedevelopers.com/magazine/107-making-data-warehouses-fast-a-developer-s-story) ## Related Jobs - [Principal Software Engineer, Database Infrastructure](https://www.wearedevelopers.com/jobs/ext/1465908-principal-software-engineer-database-infrastructure) at **GitHub** - [Senior Software Engineer, Client Apps Platform](https://www.wearedevelopers.com/jobs/ext/1773893-senior-software-engineer-client-apps-platform) at **GitHub** - [Staff Software Engineer, Database Infrastructure](https://www.wearedevelopers.com/jobs/ext/1470125-staff-software-engineer-database-infrastructure) at **GitHub** - [Staff Software Engineer](https://www.wearedevelopers.com/jobs/ext/1425755-staff-software-engineer) at **GitHub** - [Senior Data Engineer](https://www.wearedevelopers.com/jobs/ext/1589390-senior-data-engineer) at **Douglas GmbH** - [Senior Software Engineer](https://www.wearedevelopers.com/jobs/ext/15942-senior-software-engineer) at **GitHub**