Tactical Design by Example - Using Kotlin and Spring Boot (Part 6) - ArchUnit

Today I experimented a little with ArchUnit, in order to validate that an application follows our team's interpretation of Hexagon Architecture. I wanted to ensure that

  • repositories, consumers and controllers are in the corresponding adapter package
  • application services are in the application service package
  • commands and queries are in the incoming port package
  • the outgoing port package only contains interfaces
  • the domain does not use any outer layers
  • the application services do not use any adapters

I started by watching a very nice introductory talk by Alexander Schwartz

He had a promising example for validating onion architectures (just another name for Hexagon Architectures), but more about that later. It is really simple to pull off.


Test dependencies

First of all, you need to add ArchUnit as test dependencies


Test class

ArchUnit tests are, unsurprisingly, unit tests. Here's a simple test class

    packages = ["com.rewe.digital.fulfillment.delivery.depositservice"],
    importOptions = [ImportOption.DoNotIncludeTests::class]
class ArchUnitTest {

Here I make sure that only classes in specified packages are tested, excluding test classes.

Validating adapters

I want to make sure that all components that are repositories, Kafka consumers or controllers are in the corresponding adapter package.

val `repositories are adapters` = ArchRuleDefinition.classes()

val `controllers are adapters` = ArchRuleDefinition.classes()

val `consumers are adapters` = ArchRuleDefinition.classes()

The DSL makes it really easy to find the correct syntax. It's perfectly readable and offers a huge variety of options.

Validating the application layer

We use a super class for commands, queries and application services.

val `commands are incoming ports` = ArchRuleDefinition.classes()

val `queries are incoming ports`: ClassesShouldConjunction = ArchRuleDefinition.classes()

val `outgoing ports are always interfaces` = ArchRuleDefinition.classes()

val `application services are workflows` = ArchRuleDefinition.classes()

Validating the boundaries

And now we want to make sure that the boundaries are tight.

val `hexagon architecture should be enforced` = Architectures.onionArchitecture()
    .adapter("adapter", "..adapter..")

I had to include withOptionalLayers here because we don't use domain services in this example. Apparently ArchUnit expects every application to have Domain Services in a Hexagon Architecture.

Another thing: In the talk I mentioned above, this kind of validation is done "by hand", but when you check the source code of ArchUnit, the onionArchitecture() method is merely syntactic sugar for layeredArchitecture().

Very good documentation, only one drawback

The documentation is impressively extensive. There are also a lot of helpful examples on GitHub.

A minor drawback is that IntelliJ IDEA is unable to execute a single ArchUnit test in a suite. You always have to execute all tests. Maybe upvote the issue and it will get fixed.

I liked the point in Alexander Schwartz' talk about watching the watchers - making sure that the tests actually validate what they promise. I started with red tests and fixed them, but I could definitely improve there.

In the end, I found ArchUnit really easy to use. It took me less than an hour to get it working, and I'm more of a clumsy guy.

I would like to hear if any of you are using it, and what architecture rules you are enforcing. Let me know! Just contact me on Twitter.

Move on to part seven or go back to part five.