Quick reference
ArchUnit first steps
Import compiled code, describe a constraint, then execute the check.
Vocabulary
- Bytecode
- The compiled JVM class files ArchUnit inspects. Source changes matter after compilation.
- Dependency
- A recorded relationship between types, such as a field type, method signature, inheritance relationship, annotation, or method call. It is broader than a call made during a running request.
- Imported classes
- The classes selected for analysis. The test's import scope and exclusion settings determine which application code is checked.
- Predicate
- A selection criterion, such as residing in a package. The fluent rule's
that()clause applies predicates to the subjects. - Condition
- The requirement checked for the selected subjects, introduced by
should(). - Violation
- A failure of the condition. A failure report can contain several descriptions for one class.
- Baseline
- Stored descriptions of accepted existing violations of a frozen rule.
Primary reading: ArchUnit concepts and rule language.
Read the package pattern
| Pattern | Meaning |
|---|---|
com.example.orders.domain | That exact package. |
com.example.orders.domain.. | The domain package and its descendants. |
..domain.. | A domain package segment with any number of segments before or after it. |
These are ArchUnit package patterns, not Java regular expressions. Official examples.
The operations used in the first lessons
ClassFileImporter.importPackages(...)- Purpose: load the selected compiled packages for analysis.
Input: package names and the importer's options.
Output:
JavaClasses, ArchUnit's model of the imported classes. Side effects: reads classpath resources and may resolve related types from the classpath. ArchRule.check(classes)- Purpose: assert an architecture rule in a test. Input: the imported classes. Output: no return value; a rule violation raises an assertion failure. Side effects: an ordinary rule does not write a baseline. A frozen rule can write its store.
FreezingArchRule.freeze(rule)- Purpose: evaluate a rule against saved violation descriptions. Input: an existing rule. Output: a wrapped rule. Side effects: wrapping alone does not evaluate it. Subsequent evaluation reads and, when needed and permitted, writes the configured violation store.
When a rule surprises you
Confirm test discovery, compilation, imported scope, selected subjects, and the exact dependency named in the report. The default empty-selection failure catches some mistaken package patterns; it does not establish that your import covers every intended module. Test a forbidden example deliberately.
Reflection, dynamically generated proxies, external service calls, and runtime wiring can require other tests. ArchUnit's static class model does not establish that Spring can start or that an HTTP contract is compatible.