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
domain
package. 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.model
package 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.service
package contains all logic of the domain. Classes in this package use the classes indomain.model
package 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
application
package. Examples are global settings for date and time, e.g. a globalClock
object, or security related configuration that is not part of an individual adapter. Classes in this package can access thedomain
package but do not depend on any other classes in the root package. - All external dependencies are placed in dedicated
adapter
packages. Examples are anadapter.persistence
package that contains logic for storing and retrieving data from a data store oradapter.cli
andadapter.rest
packages for user interactions with the application. Classes in anyadapter
package can access all classes in thedomain
andapplication
package 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.