7 Enforcing the architecture as executable specification
Architectural rules by themselves are easy to describe but hard to keep in place once a codebase grows. This chapter shows how to turn the intended hexagonal boundaries into executable tests with ArchUnit, so violations are detected automatically during the build instead of being discovered later by manual code review. Maven modules still do useful work by preventing illegal module-to-module imports at compile time, but ArchUnit fills the gaps by checking relationships that remain technically legal to the compiler, such as wrong constructor dependencies, misplaced annotations, broken naming conventions, and incorrect generic signatures.
The chapter builds this verification layer around a configurable setup so the same rules can be reused across projects. Package boundaries and allowed libraries are externalized in YAML, then injected into checker classes through Spring Boot. From there, the rules are organized by architectural role: inner layers use allowlists, adapters use denylists, and annotations are checked so each stereotype appears only where it belongs. Structural rules go further by ensuring ports are interfaces, commands and queries implement the right marker types, validation factories exist with the correct signatures, and use case interfaces carry the correct generic parameters. Custom predicates and conditions make it possible to express rules that the fluent DSL alone cannot capture.
The result is a complete test suite that acts like living architectural documentation. Each checker is wired into a thin Spring Boot test class that scans the relevant packages and delegates to ArchUnit, so the rules remain isolated, readable, and easy to run in CI or locally. The chapter also explains how to adopt these rules safely in an existing system using ArchUnit’s freeze mechanism, which records current violations and fails only on new ones until the backlog is cleaned up. In the end, the architecture becomes something the build can enforce continuously, keeping the code aligned with the intended design.
ArchUnit tests surround the production architecture as a test-time verification layer. They act as executable documentation of the intended boundaries and help prevent architectural erosion.
The assembly module is the only component that sees everything and depends on all others.
Dependency rule strategy. Inner layers define what they may depend on (allowlist). Adapters define what they must not depend on (denylist). Repository implementations are the sole exception—they must reference domain entities to implement their contract.
Structural rule coverage by layer. Commands and queries carry the most structural constraints (five rules each). Input ports enforce interface shape and generic types. Other layers rely primarily on dependency and annotation rules.
Test execution structure. Each test class wires a specific checker to its target packages. The assembly module provides the classpath visibility that makes cross-module analysis possible. All checkers read their configuration from the same YAML properties.
Summary
- Maven modules enforce dependency direction at compile time—preventing wrong imports. ArchUnit enforces architectural relationships at test time—preventing wrong coupling, wrong stereotypes, and wrong naming within the classpath.
- Externalized YAML configuration makes rules portable across projects. Package names and allowed dependencies live in configuration, not in rule source code.
- Dependency rules use allowlists for inner layers (shared kernel, domain, handlers) and a denylist for adapters. Repository implementations are the sole exception to the adapter denylist.
- Annotation rules enforce both forbidden stereotypes (no @ApplicationService on a command) and required stereotypes (@UseCase on every input port, @OutputPort on every output port, @ApplicationService on every handler).
- Structural rules enforce the shape of commands, queries, and ports: marker interface implementation, validateThenCreate factory methods returning Either<Error, T>, no public default constructors, correct generic type arguments on use case interfaces.
- Naming convention rules guarantee that every handler implementing a @UseCase interface follows the [VerbNoun]Handler pattern derived from the interface name.
- Custom ArchCondition and DescribedPredicate implementations extend the fluent DSL when built-in methods are insufficient—covering method signature inspection, constructor analysis, and generic type position validation.
- All rules execute in the spring-boot-assembly module, where the full classpath is visible. Each test class wires a checker to its target packages through a minimal Spring Boot test context.
- The freeze mechanism enables incremental adoption: existing violations are recorded and tolerated while new violations fail the build immediately. The violation store shrinks as the team fixes legacy issues.
Clean Applications with Hexagonal Architecture ebook for free