Video in progress
What do you do so that the architecture rules the team agreed on (layers, patterns) don't erode over time?
I enforce rules in tests, not in documents: with ArchUnit, layer dependencies, naming and forbidden imports are checked in CI. The document (an ADR) answers the question "why".
📖 Answer
A wiki rule like "a controller must not call a repository directly" gets broken after three months, not out of bad intent but under deadline pressure. You can't catch everything in review either.
ArchUnit turns such rules into ordinary JUnit tests:
@AnalyzeClasses(packages = "uz.acme.billing")
class ArchitectureTest {
@ArchTest
static final ArchRule layers = layeredArchitecture().consideringAllDependencies()
.layer("Web").definedBy("..web..")
.layer("Application").definedBy("..application..")
.layer("Domain").definedBy("..domain..")
.whereLayer("Web").mayNotBeAccessedByAnyLayer()
.whereLayer("Application").mayOnlyBeAccessedByLayers("Web")
.whereLayer("Domain").mayOnlyBeAccessedByLayers("Application", "Web");
@ArchTest
static final ArchRule noServiceLocator = noClasses()
.should().dependOnClassesThat().areAssignableTo(ApplicationContext.class)
.because("dependency'lar konstruktor orqali kelsin");
}
What works when several teams use it:
- the rules live in a shared test library, and each service wires them into its own tests;
- when adding to an existing project, use
FreezingArchRule: old violations get "frozen" and new ones are forbidden. Otherwise you get 400 errors on day one and everybody turns the rules off; - give every rule a
because(...)explanation, so whoever sees the failing test understands the reason right away; - fewer rules, but more important ones. With 50 rules, the team looks for ways around them.
Spring Modulith gives you a similar check: dependencies between modules go only through the public API, and there are no cycles.
ADRs are needed too, but for a different purpose: when a decision was made, why, and with which alternatives. If you notice a rule has become wrong, you don't delete the test: you write a new ADR and change the rule openly.
Sign up to mark studied questions and save your progress.