0Pricing
Clean Architecture & Design Patterns in Practice · Lesson

Architectural Fitness Functions and Boundary Tests

Learn to protect architectural boundaries over time using automated fitness functions and dependency-direction tests.

Architectural Fitness Functions and Boundary Tests is a free Clean Architecture & Design Patterns in Practice lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Clean Architecture & Design Patterns in Practice learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Architecture Erodes Silently

A clean design tends to decay. Under deadline pressure, someone imports the database into an entity, or a use case reaches into the web layer.

Code review alone misses these. We need automated guards.

What Is a Fitness Function?

An architectural fitness function is an automated test that asserts a structural property of the system.

Just as unit tests guard behavior, fitness functions guard architecture — for example, the direction of dependencies.

The Rule We Want to Enforce

In Clean Architecture, dependencies point inward:

  • Entities depend on nothing.
  • Use cases depend only on entities.
  • Frameworks depend inward, never the reverse.

A fitness function can fail the build if this is broken.

Expressing It as a Test

Tools like ArchUnit let you encode the rule directly.

ArchRule rule = classes()
    .that().resideInAPackage("..domain..")
    .should().onlyDependOnClassesThat()
    .resideInAnyPackage("..domain..", "java..");

Forbidding Forbidden Imports

You can also assert that the core never touches infrastructure.

noClasses()
    .that().resideInAPackage("..usecase..")
    .should().dependOnClassesThat()
    .resideInAPackage("..web..");

Boundary Contract Tests

Beyond dependency direction, test that adapters honor their port contracts.

Run the same suite against every implementation of a gateway so a new adapter cannot silently break the boundary.

A Simple Home-Grown Check

Even without a library you can scan for violations programmatically.

public class Main {
  public static void main(String[] a){
    String[] domainImports = {"java.util.List", "domain.Order"};
    boolean clean = true;
    for (String imp : domainImports) {
      if (imp.startsWith("web.") || imp.startsWith("db.")) { clean = false; }
    }
    System.out.println("Domain layer clean: " + clean);
  }
}

Running Them in CI

Fitness functions belong in the continuous integration pipeline.

When a pull request breaks a boundary, the build goes red immediately — long before the violation spreads through the codebase.

Choosing the Right Functions

  • Layer dependency direction.
  • No framework imports in the core.
  • Naming and package conventions.
  • Cyclic-dependency detection.

Start with the few rules that matter most for your design.

Evolving the Rules

Fitness functions are living. As the architecture intentionally evolves, update the rules to match the new intent.

A failing fitness function is a prompt to decide: fix the code, or consciously change the rule.

Balancing Strictness

Too many brittle rules cause friction and get disabled. Too few let decay creep in.

Aim for a small set of high-value, stable rules that protect the boundaries you care most about.

Quick Check

Test your understanding of fitness functions.

Recap

You learned to defend boundaries over time.

  • Fitness functions automate architectural rules.
  • Enforce inward dependency direction and a framework-free core.
  • Run them in CI and evolve them deliberately as the design changes.

Frequently asked questions

Is the “Architectural Fitness Functions and Boundary Tests” lesson free?

Yes — the full text of “Architectural Fitness Functions and Boundary Tests” is free to read here on the web, and the Clean Architecture & Design Patterns in Practice course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Clean Architecture & Design Patterns in Practice course, upgrade to CoddyKit PRO.

What will I learn in “Architectural Fitness Functions and Boundary Tests”?

Learn to protect architectural boundaries over time using automated fitness functions and dependency-direction tests. You practise Clean Architecture & Design Patterns in Practice with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Clean Architecture & Design Patterns in Practice?

No prior experience is required. Clean Architecture & Design Patterns in Practice on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Architectural Fitness Functions and Boundary Tests” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Clean Architecture & Design Patterns in Practice lesson?

Yes. Every Clean Architecture & Design Patterns in Practice lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Layered Testing Strategy
  2. Deployment Considerations for Clean Arch
  3. Evolving and Maintaining Clean Systems
  4. Architectural Fitness Functions and Boundary Tests
← Back to Clean Architecture & Design Patterns in Practice