Dependency direction and architecture tests
What you will learn
Architecture tests turn a dependency convention into a regression check. Choose a rule that protects a real boundary and test both an accepted and a rejected dependency.
- Represent permitted dependency directions and detect cycles.
- Distinguish JPMS compiler enforcement from architecture analysis.
- Explain how ArchUnit and Spring Modulith complement build boundaries.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 models; the companion JPMS project has separate build instructions.
An explicit dependency policy
public class Main { static boolean allowed(String from, String to) { return from.equals("orders") && to.equals("inventory.api"); } public static void main(String[] args) { System.out.println(allowed("orders", "inventory.api")); System.out.println(allowed("orders", "inventory.internal")); System.out.println(allowed("inventory", "orders.api")); }}Expected output:
truefalsefalseThe whitelist models a deliberately small cross-feature policy. Intra-module dependencies are outside this predicate. A real test must extract the actual dependencies.
Dependency direction
Domain rules should not require HTTP, database or framework details. An application use case invokes a port; an adapter implements it. The composition root is permitted to know concrete adapters. A dependency cycle makes independent change and testing harder. A directed acyclic graph permits an ordering of modules, but does not prove good domain design.
Enforcement options
JPMS uses requires and exports to control module readability and access; a public class in an unexported package is inaccessible to ordinary external source code. Maven and Gradle subprojects make build dependencies explicit but do not inherently enforce every package boundary. ArchUnit can inspect compiled class dependencies and assert layer or slice rules; Spring Modulith can verify application-module cycles, internal access and optional allowed dependencies. Neither external framework is executed in this batch.
Review the rule itself
For ArchUnit, a source-reviewed candidate is slices().matching("com.example.(*)..").should().beFreeOfCycles() (static import from SlicesRuleDefinition); import the intended compiled packages first. For Spring Modulith, ApplicationModules.of(Application.class).verify() checks discovered modules in a configured project. These snippets require their respective libraries. A passing graph model is not evidence that actual bytecode matches it. The companion lab separately compiles real modules and inspects dependencies with jdeps.
Detect cycles in a declared graph
public class Main { static boolean cyclic(java.util.Map<String,java.util.List<String>> g) { var active = new java.util.HashSet<String>(); var done = new java.util.HashSet<String>(); for (String n : g.keySet()) if (visit(n,g,active,done)) return true; return false; } static boolean visit(String n, java.util.Map<String,java.util.List<String>> g, java.util.Set<String> active, java.util.Set<String> done) { if (active.contains(n)) return true; if (done.contains(n)) return false; active.add(n); for (String next : g.getOrDefault(n,java.util.List.of())) if (visit(next,g,active,done)) return true; active.remove(n); done.add(n); return false; } public static void main(String[] args) { var g = new java.util.HashMap<String,java.util.List<String>>(); g.put("orders", java.util.List.of("inventory")); g.put("inventory", java.util.List.of()); System.out.println(cyclic(g)); g.put("inventory", java.util.List.of("orders")); System.out.println(cyclic(g)); }}Expected output:
falsetrueThe active set records the current depth-first path; encountering it again reveals a cycle. The done set avoids repeating completed work. This small recursive teaching model is not designed for arbitrarily deep graphs.
A contract test independent of an adapter
public class Main { interface Stock { int available(String sku); } static class Memory implements Stock { private final java.util.Map<String,Integer> data = java.util.Map.of("PEN",4); public int available(String sku) { return data.getOrDefault(sku,0); } } static void check(Stock s) { if (s.available("PEN") != 4 || s.available("BOX") != 0) throw new AssertionError("stock contract"); } public static void main(String[] args) { check(sku -> sku.equals("PEN") ? 4 : 0); check(new Memory()); System.out.println("two adapters accepted"); }}Expected output:
two adapters acceptedThis checks behavior across two adapters, not the source dependency graph. Both kinds of checks matter.
Common mistakes
Do not test a hand-written graph and call it bytecode verification. Do not enforce package rules so broadly that composition roots cannot wire the application. Dynamic reflection and external configuration need separate review.
Practice
Write a dependency rule for billing: it may import orders.api but not orders.internal. Print true for the first and false for the second.
Expected output:
truefalseCompare with the manual review criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dependency direction and architecture tests — knowledge check
Modular architecture and contract reasoning.
What does a dependency cycle mean?
What does exports in JPMS control?
Which tool examines compiled class dependencies?
What does a passing hand-written graph test prove?
Where should concrete adapter wiring normally live?