Testing Error Propagation Across Boundaries
You'll learn how to verify that errors from external systems are properly caught, translated, and propagated through your module boundaries.
The Core Idea
When your code integrates with external systems (databases, APIs, file systems), errors will occur—network failures, invalid responses, timeouts, etc. Your integration layer acts as a translator: it catches these external errors and converts them into domain-appropriate exceptions or error states that the rest of your application can understand. Testing error propagation means verifying this translation happens correctly at the boundary.
Think of it like a diplomatic interpreter at a border crossing. When a foreign official delivers bad news in their language, the interpreter must accurately translate both the message and its severity so your side understands what went wrong and can respond appropriately.
Why This Matters
Without proper error translation, your domain logic becomes coupled to external error formats. A database throwing a SQLException shouldn't leak into your business layer—it should become a DataAccessException or similar domain-appropriate error. Testing ensures:
- External errors are caught at the boundary
- They're translated into meaningful domain errors
- The translation preserves important context
- Downstream code receives errors it can handle
- Implementation details don't leak through
Testing Strategy
In your integration tests (testing adapter layers and cross-module communication), simulate external failures and verify the transformation:
- Mock the external system to throw its native errors
- Assert your boundary code catches them
- Verify the correct domain exception emerges
- Check that relevant context (error messages, codes) is preserved
- Ensure unexpected errors are also handled gracefully
This is a narrow integration test focused specifically on the error-handling contract of your boundary.
Key Takeaway: Test that your module boundaries act as responsible translators—catching external system errors and converting them into meaningful domain exceptions that protect your application's internal layers from external implementation details.