Generic Architecture Tests written in Kotlin using ArchUnit and Junit5
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:
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:
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.domain.service
package contains all logic of the domain.
Classes in this package use the classes in domain.model
package but do not have any dependencies to any other classes in the root package.application
package.
Examples are global settings for date and time, e.g. a global Clock
object, or security related configuration that is not part of an individual adapter.
Classes in this package can access the domain
package but do not depend on any other classes in the root package.adapter
packages.
Examples are an adapter.persistence
package that contains logic for storing and retrieving data from a data store
or adapter.cli
and adapter.rest
packages for user interactions with the application.
Classes in any adapter
package can access all classes in the domain
and application
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.