Architecture Fitness Functions: Check Design Constraints

Your team agreed that domain code stays out of the persistence package. It's written down, it came up in the last design review, and a pull request just added an OrderRepository field to OrderService anyway. Review missed it because the diff was mostly about something else.
Architecture fitness functions turn that kind of agreement into a build check. A small ArchUnit example shows how the rule catches a dependency, how a baseline separates existing debt from new violations, and how exceptions stay tied to the architectural decision.
Turn one architectural constraint into a measurable contract
The term comes from Neal Ford, Rebecca Parsons, and Patrick Kua's Building Evolutionary Architectures. The book's definition, which Thoughtworks put on its Technology Radar in 2017, is "an objective integrity assessment of some architectural characteristics." In practice, a fitness function tells you how close the system is to an architectural aim. It targets architectural goals and constraints, which can change independently of the features your other tests cover.
"Keep the architecture clean" can't fail a build. A specific constraint can. For the order application, the domain package must not depend on persistence implementations, so business rules aren't tied to one storage adapter. Before writing any code, write down six things:
- The decision: domain code must not reference
example.persistenceclasses. - What the check looks at: compiled production classes in this module.
- The invariant: zero new domain-to-persistence dependencies.
- The trigger: every build, plus any change to the rule itself.
- The owner: the orders team.
- The response: fix the dependency, fix the check, or open a reviewed exception.
That record works for any quality attribute, but each kind of architectural goal needs different evidence:
A green dependency check says nothing about checkout latency, and a passing load test says nothing about package structure. For the wider practice this fits into, see our guide to architecture as code. This post stays with one rule and what it takes to keep it honest.
Choose the check boundary before choosing a tool
A Java dependency rule and a latency objective can come out of the same architecture review, but they read completely different inputs. Pick the input first, because it decides what a green result can claim.
ArchUnit sits in the first row. It's static analysis of Java bytecode: it imports compiled classes into a code structure and checks rules against it. So the import decides what the rule can see. For this example, import the production classes under example and leave tests out. Then decide whether generated classes, adapters in other modules, and external JARs belong in the scope, and write that down next to the rule. If the rule ever fails on a test fixture, suspect the import before you suspect the architecture.
Then ask whether a change could break the decision without creating the dependency this rule rejects. Here it can. We gave OrderService a java.sql.Connection and an inline insert statement, and the persistence-package rule still passed, because nothing in example.persistence was referenced. The intent was broken; the rule's forbidden dependency never appeared. Keep the rule, and add a second one or a review question that covers storage APIs in domain code.
Thoughtworks' enterprise architecture playbook lists several ways to classify fitness functions. Three of them are worth using when they change what you build:
- Atomic vs holistic: atomic fitness functions test one architectural concern, like this package boundary. Holistic fitness functions test several together, such as latency and data freshness under the same workload.
- Triggered vs continual: triggered fitness functions run on an event like a CI build. Continual ones (often called continuous fitness functions) keep gathering data from the running system. Running a build check on every commit doesn't make it continual, because it still runs on a discrete event.
- Automated vs manual: manual fitness functions need a person to execute them. The same playbook argues that manual checks of key decisions are worth more than automated ones aimed at low-hanging fruit. For a manual review, record who made the call, what they looked at, and what they decided, so "reviewed" means more than "we had a meeting."
How a dependency check makes failures actionable
ArchUnit runs as a regular unit test. It ships JUnit 5 and 6 support; with Maven, add the JUnit 6 artifact as a test dependency:
With Maven's JUnit Platform support, mvn test runs the rule below. This JUnit 6 setup needs Surefire 3.0.0 or later.
The tested example has three classes:
example.domain.OrderServiceexample.domain.CheckoutServiceexample.persistence.OrderRepository
Here's the whole test:
@AnalyzeClasses sets the import, and DoNotIncludeTests keeps test classes out of it. The rule uses ArchUnit's package dependency syntax: select the domain classes, reject any dependency on the persistence package. If your constraint is about cyclic dependencies rather than direction, slices().matching("example.(*)..").should().beFreeOfCycles() from the same library does that job; we ran it against a deliberate domain-to-persistence cycle and it failed as expected.
With empty domain classes, the test passes. Add one line to OrderService:
The build fails, and Surefire's report shows the rule, the reason and the offending field:
The :0 is expected for a field-type dependency: class files don't store a source line number for field declarations. The field name still tells you which dependency broke the rule.
Keep both versions of that fixture, a conforming one and a violating one, and run them whenever the rule or its import changes. A rule that passes on clean code proves very little if nobody has checked that it still fails on the dirty version.
Then make the failure easy to act on. The comment above the test names the decision record and the owning team, so whoever hits the red build can see which dependency broke the rule, why the rule exists and who can approve an exception. Keep that pointer in a comment, not in the .because() text, for a reason the next section makes clear. If editing the assertion is the quickest way to find out who owns it, someone will edit the assertion.
Introduce a baseline without accepting new violations
A new rule in a grown codebase usually fails on day one. The ArchUnit guide is blunt about it: rules introduced in grown projects often hit "hundreds or even thousands of violations." Fixing all of that technical debt before the rule can merge means the rule never merges. Turning the rule off means tomorrow's regression gets in too.
FreezingArchRule handles this. It records today's violations in a violation store, reports only new ones on later runs, and removes each stored violation once it's fixed. Wrap the rule and point the store at a version-controlled path:
Store creation is already off by default; setting it explicitly documents the intent. Create the baseline once, on purpose, with mvn test -Darchunit.freeze.store.default.allowStoreCreation=true. Without that flag and without a committed store, the test errors with StoreInitializationFailedException: Creating new violation store is disabled, which is what you want on a fresh CI checkout.
The baseline run left two files: stored.rules, which maps the full rule description to a UUID-named file, and that file, holding one entry per frozen violation. Commit both and review them like code. Here's what happened next, with the store carried between runs:
That's the ratchet: the debt can shrink, and what you've fixed stays fixed. Matching is per violation, not per class, so a new method call added to the frozen OrderService still fails the build. That's the difference from ArchUnit's ignore patterns: when we ignored .*example\.domain\.OrderService.*, the same new call passed silently. A frozen fitness function treats the baseline as a list of specific debts. A class-wide ignore pattern hides everything in that class, including next month's mistakes.
The ratchet only holds if the store does, and there are four places it slips:
- Commit the shrink. The repair run rewrites the store in your workspace. When we restored the pre-repair store, as a fresh CI checkout would, the reintroduced field passed again. Make "commit the smaller store" part of your definition of done for debt fixes.
- Treat rule descriptions as baseline changes. The store keys each rule by its full description, including the
.because()text. When we reworded the reason, the run passed with the violation present,stored.rulesgained a second entry, and the old one stayed behind as an orphan. - Don't refreeze in CI.
freeze.refreeze=trueoverwrites the store with whatever exists now and reports success. Our refreeze run passed with a fresh regression in it. - Expect renames to look new. The default matcher ignores line numbers and the numbers of anonymous classes and lambdas, but renaming the field to
repoproduced a new violation. Review before-and-after violations during refactors.
You can lock the store with freeze.store.default.allowStoreUpdate=false, but know what it does. Fixing a frozen violation then breaks the build, because ArchUnit can't remove the solved entry:
The upside is that a reworded rule fails the same way instead of quietly starting a fresh baseline. If you lock the store in CI, pair it with a reviewed step that runs with updates enabled and commits the smaller store. Either way, a frozen pass means "no new violations." The old ones still belong on someone's backlog, and your architecture reviews should keep checking that the list gets shorter.
Give exceptions an owner, evidence, and an expiry condition
A red check needs a diagnosis before it needs a waiver. The cause might be a real regression, an import that swept in test helpers, or a sign that the architecture decision itself has moved. For runtime fitness functions, repeated failures can also mean an unstable workload or missing measurements. Each of those needs a different fix, so triage first:
- Confirm what the check actually looked at. Which classes, config, workload, or proposal did it see, and should it have seen them?
- Check the decision. Does the constraint still apply, or has a later decision superseded it?
- Pick the outcome: fix the code, fix the check, or open a bounded exception. Don't collapse all three into "ignore this failure."
When an exception is the right call, here's a lightweight record you can adapt:
Put a real name in the owner field. A milestone-based expiry also needs someone who notices when the milestone slips, or it turns into permanent permission.
Link the exception to the architecture decision record it deviates from. ADRs are immutable once accepted, so if the team has moved the boundary on purpose, write a new ADR that supersedes the old one and update the rule and baseline with it. If the boundary still holds, the exception explains why this one class is temporarily outside it. Either way, the exception shouldn't rewrite the constraint for every other class.
Then track whether the check earns its place. Record which failures led to a code fix, a scope fix, or an approved decision change, and which exceptions passed their review condition unresolved. Thoughtworks' playbook has a line for checks nobody acts on: they're "not different to normal broken tests: adding waste, not value." For runtime architectural characteristics, keep acceptable latency, error rate, and workload context in the same record, so an improvement in one number under a lighter workload doesn't get logged as an architecture win.
Keep the architecture decision and the executable check aligned
A dependency rule can't tell you whether the boundary is still the right design. Suppose the order team drops the repository abstraction in a later migration. Leaving the old rule in place creates friction for no reason, and deleting it without updating the decision leaves a hole in the history.
Use an explicit loop: decision → scoped check → failure or exception review → revised decision. Each revision should record what changed, why, who accepted it, and which build proved the new rule works. Then update the rule and its baseline together, and rerun the conforming and violating fixtures.

A passing frozen rule covers only what it imported and its reviewed baseline. Review can lead to a code or scope correction, a bounded exception, or a revised architecture decision.
Evolutionary architecture assumes boundaries will move, so this loop never really finishes. Our workflow runs Understand → Decide → Design → Execute → Compound, and we provide the decision and design layer: Catio doesn't operate your runtime systems and isn't an observability or DevOps tool. In the Compound stage, Catio flags where execution has diverged from intent and raises drift as a Recommendation, which naturally prompts you to ask whether a frozen rule and its decision still match. Earlier, our GitHub source integration maps services, modules, dependencies, and integration edges from code, which helps you pick the boundaries worth encoding as architectural fitness functions in the first place.
When the boundary moves, our MCP integration lets a coding assistant search inventory relationships, pull the relevant design, and record the revised decision: edit_blueprint_with_archie drafts the change and update_blueprint saves it once you've reviewed it. It can't delete Blueprints, and inventory and workspace context are read-only. None of this runs your ArchUnit check, so keep the build result with the implementation review.

Archie drafts the Blueprint change; it's saved with update_blueprint only after review. Visual from our MCP walkthrough (full-size image).
The pattern is a rule that catches a specific dependency, a baseline that separates existing debt from new violations, and an exception process with an owner. Get that working for one constraint, with someone maintaining the baseline and a failure people know how to respond to, before you write the second. The decision behind the rule is where its reason should live.
Want the decision behind each check, and its history, in one place? Book a demo with us.