This repository shows generic examples for architecture tests using ArchUnit. The tests are written in Kotlin using the Junit5 testing framework.
The term onion architecture was first used by Jeffrey Palermo in his onion architecture blog post. Other names for similar architectural approaches are hexagonal architecture, ports and adapters, and clean architecture.
We built ArchUnit tests for this architectural style in the OnionArchitectureTest.kt file.
It assumes a certain structure of all classes in the root package, i.e. the package of the OnionArchitectureTest class:
- The core domain should be placed in the
domainpackage. It contains the code logic of the application and is is independent of the infrastructure, adapters, frameworks in use, etc. We divide the package into two parts:- The
domain.modelpackage contains all models of the domain. Classes in this package do not have any dependencies to any other classes in the root package. - The
domain.servicepackage contains all logic of the domain. Classes in this package use the classes indomain.modelpackage but do not have any dependencies to any other classes in the root package.
- The
- All application related parts, i.e. technical logic needed for the application to run and that are not part of the core domain,
should be placed in the
applicationpackage. Examples are global settings for date and time, e.g. a globalClockobject, or security related configuration that is not part of an individual adapter. Classes in this package can access thedomainpackage but do not depend on any other classes in the root package. - All external dependencies are placed in dedicated
adapterpackages. Examples are anadapter.persistencepackage that contains logic for storing and retrieving data from a data store oradapter.cliandadapter.restpackages for user interactions with the application. Classes in anyadapterpackage can access all classes in thedomainandapplicationpackage but must not have dependencies on any other adapter packages.
Note that the example code in the onion package contain code that breaks the build. Uncomment the corresponding parts to make the tests pass.
Let us know if you like ArchUnit and use the templates in this repository by leaving a tweet mentioning @archtests and @spanier_m.
If you want to change parts of these examples, simply open a pull request with the changes and/or open an issue.