|
| 1 | +# Testing using hexagonal architecture |
| 2 | + |
| 3 | +Use a bank example to demonstrate how using a hexagonal architecture can help to test an application. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +The application is split into 3 modules: |
| 8 | + |
| 9 | +- domain: business logic driving how the bank behaves. |
| 10 | +- http: api to expose the bank functionality via http. |
| 11 | +- web: dummy web ui driving the application. |
| 12 | + |
| 13 | +## Solution |
| 14 | + |
| 15 | +The business logic of the bank is fully implemented as part of the domain. As per typical hexagonal architecture, where |
| 16 | +an interaction is needed with the outside world, a contract (port) is defined to state how the application expects that |
| 17 | +to happen. In this example only 2 are defined: |
| 18 | + |
| 19 | +- one that allows to drive the application by exposing all the banks capabilities - driving or primary port; |
| 20 | +- and another to define how the bank accounts data is stored and queried - driven or secondary port. |
| 21 | + |
| 22 | +The isolation of the business logic that the hexagonal architecture provides, allows components to have a single |
| 23 | +purpose, only mapping from the technology in use to business terms. This is considered one of its main benefits but here |
| 24 | +we attempt to show how it can also greatly help testing. |
| 25 | + |
| 26 | +Having every component in the system defined in terms of contracts means that writing tests based on those very same |
| 27 | +contracts allows to have a single suite of tests that can be run against any layer of the system, providing excellent |
| 28 | +test coverage for the whole system with small effort. |
| 29 | + |
| 30 | +This example mainly focuses in the contract defining how the business behaves as it highlights the benefits of the |
| 31 | +approach, but the same strategy can be applied to any other port. For examples, if there were multiple ways to |
| 32 | +store/query accounts, a single test could be defined to test all of them. |
| 33 | + |
| 34 | +## Technology |
| 35 | + |
| 36 | +The intention is to have as simple as possible code so very few external dependencies are used. It's implemented in |
| 37 | +[kotlin](https://kotlinlang.org), my preferred programming language, and the only production dependency is the |
| 38 | +[http4k](http://http4k.org/) library, which is used to define the http api as well as the server templating in the ui. |
| 39 | + |
| 40 | +For testing, only [junit5](https://junit.org/junit5/) and [hamkrest](https://github.com/npryce/hamkrest) are used. |
0 commit comments