Enforcing Module Boundaries with Architecture Tests
Keep a modular monolith modular: enforce boundaries in CI with deptrac, dependency-cruiser, ArchUnit, import-linter and Spring Modulith.
Published November 27, 2026
Every modular monolith starts with clean boundaries. Then a deadline arrives, someone imports another module's repository "just this once", and six months later the modules are modules in name only. The only reliable defence is automation: architecture tests that run in CI and fail the build when a forbidden dependency appears.
The concept
Architecture tests analyse your code's dependency graph, usually from imports or class references, and check it against rules you define: module A may use module B's public API but not its internals, the domain layer may not depend on infrastructure, there are no cycles between modules. They run in seconds and give developers immediate feedback, which is far more effective than catching violations in code review.
Every major ecosystem has a tool. PHP has deptrac and PHPArkitect, JavaScript and TypeScript have dependency-cruiser and eslint-plugin-boundaries, Java has ArchUnit and Spring Modulith, .NET has NetArchTest and ArchUnitNET, and Python has import-linter. They differ in syntax but share the same idea: declare layers or modules, declare allowed dependencies, and fail on everything else.
How it works
Tools by ecosystem
| Language | Tool | Rule style |
|---|---|---|
| PHP | deptrac, PHPArkitect | YAML layers + allowed dependencies |
| JavaScript / TypeScript | dependency-cruiser, eslint-plugin-boundaries, Nx module boundaries | JSON/JS rules on import paths |
| Java / Kotlin | ArchUnit, Spring Modulith | Tests written in code |
| .NET | NetArchTest, ArchUnitNET | Fluent assertions in tests |
| Python | import-linter | INI/TOML contracts (layers, independence) |
| Go | go-arch-lint, depguard | YAML rules on packages |
Rules to start with
- Declare modules and their public entry points
- Allow: any module → other module's public API
- Forbid: any module → other module's internals
- Forbid: cycles between modules
- Start with a baseline of existing violations; fail only on new ones
- Burn down the baseline over time
Adopting on an existing codebase
Turning on strict rules in a legacy codebase produces hundreds of violations and a disabled check. Instead, record existing violations as a baseline (deptrac and dependency-cruiser both support this), fail only on new ones, and reduce the baseline gradually as part of normal work. The ratchet ensures things only get better.
Practical examples
deptrac (PHP / Laravel)
Each module may depend on others only through their Api namespace.
# deptrac.yaml
deptrac:
paths: [./app/Modules]
layers:
- { name: CatalogApi, collectors: [{ type: classLike, value: ^App\\Modules\\Catalog\\Api\\.* }] }
- { name: CatalogInternal, collectors: [{ type: classLike, value: ^App\\Modules\\Catalog\\(?!Api).* }] }
- { name: Checkout, collectors: [{ type: classLike, value: ^App\\Modules\\Checkout\\.* }] }
ruleset:
Checkout: [CatalogApi]
CatalogInternal: [CatalogApi]
# vendor/bin/deptrac analyse --fail-on-uncovereddependency-cruiser (TypeScript)
Only api.ts and events.ts of another module may be imported.
module.exports = {
forbidden: [{
name: 'module-internals-are-private',
severity: 'error',
from: { path: '^src/modules/([^/]+)/' },
to: { path: '^src/modules/([^/]+)/(?!api\\.ts$|events\\.ts$)', pathNot: '^src/modules/$1/' },
}],
}
// npx depcruise src --config .dependency-cruiser.cjsSpring Modulith verification (Java)
Detects modules from packages and verifies boundaries in one test.
class ModularityTests {
@Test
void verifiesModularStructure() {
ApplicationModules.of(ShopApplication.class).verify();
}
}Common mistakes
- Relying on code review alone to protect boundaries.
- Enabling strict rules on a legacy codebase without a baseline, then disabling the check.
- Rules that allow everything through a giant shared module.
- Running architecture tests only nightly instead of on every pull request.
- Error messages that do not explain the rule or how to fix the violation.
Key takeaways
- Boundaries erode without automated enforcement.
- Architecture tests check the dependency graph against declared rules in CI.
- Every ecosystem has a tool: deptrac, dependency-cruiser, ArchUnit, import-linter, Spring Modulith.
- Baseline existing violations and ratchet them down over time.
FAQ
Do architecture tests slow down CI?
Rarely noticeably. Static dependency analysis of a large codebase usually takes seconds to a minute.
What about runtime coupling like shared database tables?
Static tools catch code imports. Add tests or database permissions for table ownership, and review cross-module events and APIs.
Can these tools generate diagrams?
Yes. dependency-cruiser, deptrac and Spring Modulith can output dependency graphs, which make great living architecture documentation.