Mastering Test Strategies with Hexagonal Architecture

Explore how Hexagonal Architecture enhances TDD efficiency with clean code boundaries, inside-out for the domain model, outside-in for adapters, and unit-testing user stories.

The topic for this lesson is how using a hexagonal architecture impacts TDD. Knowing that we are using hexagonal architecture presents useful boundaries for the different kinds of tests in the test pyramid.

Organizing code with hexagonal architecture

In one sense, how we organize our code base does not affect our use of TDD. The internal structure of the code is simply an implementation detail, one of many possibilities that will make our tests pass. That being said, some ways of structuring our code are easier to work with than others. Using hexagonal architecture as a foundational structure does offer TDD some advantages. The reason why lies with the use of ports and adapters.

We’ve learned that it is easier to write tests for code where we can control the environment in which the code runs. We’ve seen how the test pyramid gives a structure to the different kinds of tests we write. Using the ports and adapters approach provides clean boundaries for each kind of test in the code. Better yet, it provides us with an opportunity to bring even more tests to the unit test level.

Let’s review what kinds of tests best fit each layer of software written using hexagonal architecture.

Inside-out works well with the domain model

Classic TDD uses an inside-out development approach, where we choose a certain software component to test-drive. This component may be a single function, a single class, or a small cluster of classes that collaborate with each other. We use TDD to test this component as a whole given the behaviors it offers to its consumers.

Get hands-on with 1200+ tech skills courses.