ArchUnit for Spring Boot · Lesson 02 · About 20 minutes
Freeze existing violations
Introduce a useful constraint today, then remove old violations gradually.
Prerequisite: your lesson 1 rule passes and you have proved that it catches a forbidden dependency.
Recall before reading
Where does ArchUnit get its evidence? What change did you make to prove your rule was running?
Check the idea
It inspects compiled classes. An actual forbidden type reference made the architecture test fail; removing the reference made it pass again.
Accept a known starting point
A baseline is the saved set of existing violation descriptions that you accept
temporarily. FreezingArchRule wraps a rule and compares its current violations with that
baseline. New, unmatched descriptions fail; a normal updating run removes descriptions whose
violations have been fixed. Read Freezing Arch Rules.
ArchRule frozenRule = FreezingArchRule.freeze(existingRule);
frozenRule.check(classes);
This schematic example assumes you already constructed existingRule and imported
classes. Import com.tngtech.archunit.library.freeze.FreezingArchRule.
The first successful evaluation of a previously unrecorded rule saves its current violations,
provided the store settings permit the required writes.
| Change after baselining | Expected result |
|---|---|
| Keep the recorded violation | Pass. |
| Add a different forbidden reference to that same old class | Fail on the new violation. |
| Fix the recorded violation | Pass and reduce the baseline when updates are allowed. |
| Reintroduce it after that reduction is saved | Fail. |
Freezing does not consult Git history or classify classes as old and new. It recognizes reported violations. The default matcher ignores source line numbers, so inserting blank lines usually does not turn a known violation into a new one. Matching behavior.
Exercise: complete the freeze cycle
Work in the same Maven lab and keep both existing tests. Write
FrozenServiceArchitectureTest.java from scratch in the same test package.
Give it a new rule: classes in ..service.. must not depend directly on classes
in ..web... Keep the rule's description stable throughout this exercise.
- Add an
OrderController-typed field namedlegacyControllertoOrderService. It can remain uninitialized and non-final for this static-check experiment. Before freezing, confirm that your ordinary rule fails. - Wrap the new rule yourself and call
checkonly on its frozen form. Run the one-time command below to record the starting violations, then run ordinarymvn test. Both runs should pass. Open the files inarchunit_store/and identify the field's description. - In that same service, add a second
OrderController-typed field namednewController. An ordinary test run must fail. Remove only the new field; the test should pass again. - Remove
legacyControllerand run ordinarymvn test. Confirm its recorded description disappears. Save a copy of the reduced store, then reintroduce the exact same field. The test must now fail. Remove it and finish with a passing run.
One-time initial baseline command, from exercises/architecture-lab
mvn test -Darchunit.freeze.store.default.allowStoreCreation=true
The supplied src/test/resources/archunit.properties sets creation to false,
updates to true, and refreezing to false for normal local runs. Store files live in the
lab's archunit_store/, outside disposable target/ output.
Why must I run after fixing the old field?
The stored baseline only changes when the frozen rule evaluates the repaired code with updates allowed. Deleting and restoring the field between runs leaves the original baseline intact, so it can still recognize that violation.
The number of violations surprised me
ArchUnit reports dependency evidence, which can include fields, constructor parameters, and method calls. Read the descriptions; do not equate the count with a count of files or classes.
Carry the baseline with the code
In a team repository, review and version the store alongside the rule. Include the reduced store
with a code fix so other machines enforce the same progress. Do not routinely delete the store
or enable freeze.refreeze=true: those actions can accept violations you intended to reject.
The CI reference covers read-only verification, renamed rules, and why an unrecorded baseline reduction can make a CI run fail. Keep that detail for adoption work after you have seen the local cycle.