7 Enforcing the architecture as executable specification
This chapter covers
- Translating module dependency rules into executable ArchUnit tests
- Enforcing annotation placement across hexagonal layers
- Writing custom conditions for structural shape validation
- Naming convention enforcement through predicates
- Configuring rules through externalized YAML properties
- Adopting architectural rules with the freeze mechanism
We assembled WriteFlow into a running system. Maven modules enforce dependency direction at compile time—if the domain POM does not declare Spring Framework, no class inside it can import a Spring type. But Maven modules operate at the artifact level. They prevent wrong imports. They cannot prevent wrong relationships within the classpath that the compiler considers legal. A handler's constructor can accept an adapter as a parameter—Maven will not complain, because the assembly module puts both on the classpath at test time. A command class can carry a @RestController annotation; the compiler sees a valid annotation on a valid class. A use case implementation can be named FooService instead of following the handler naming convention—nothing in the type system rejects it.
These are not compiler errors. They are architectural violations—invisible until someone reads the code and notices the drift. By then, the drift has usually spread.