ArchUnit for Spring Boot · Lesson 01 · About 15 minutes
Catch a forbidden dependency
Write a test that prevents a controller from reaching directly into persistence.
Turn a design decision into a test
Your team agrees that controllers call services, and services coordinate persistence. Java will still compile a controller that accepts a repository in its constructor. ArchUnit lets you make that unwanted dependency fail a test.
ArchUnit reads bytecode, the compiled .class files, and builds
a model of classes and their dependencies. A rule selects part of that model
and checks a condition. These tests run without starting Spring.
Read the short official getting-started guide.
Read one supplied rule
The supplied test protects a different decision: domain classes cannot depend on web classes. Its central expression is:
ArchRule domainDoesNotDependOnWeb = noClasses()
.that().resideInAPackage("..domain..")
.should().dependOnClassesThat().resideInAPackage("..web..");
Read it as one sentence: no classes in a domain package should depend on classes in a web package.
A package pattern selects package names; .. matches any number
of package segments, including zero. Thus ..domain.. also includes nested domain packages.
Package checks.
The test imports com.example.orders, excludes compiled test classes, and calls
domainDoesNotDependOnWeb.check(classes). Calling check executes the rule;
constructing the rule alone does not assert anything. The import is the code you inspect,
while the rule's that() clause selects which imported classes it governs.
Run the supplied example
Use the setup instructions if Java or Maven is missing.
cd "/home/dmedeiros/Documents/ChatGPT/Teach me ArchUnit/exercises/architecture-lab"
mvn test
Expect one passing OrientationTest. Inspect the four classes under
src/main/java/com/example/orders. The controller has a service reference;
the service has a repository reference. Our rules examine these direct dependencies.
Exercise: protect the controller's route
Write WebArchitectureTest.java from scratch under
src/test/java/com/example/orders/architecture. Use a Jupiter @Test
method and the supplied importer as a syntax reference. Your rule must forbid every class
in ..web.. from depending directly on a class in ..persistence...
- Make the rule pass on the supplied code. Confirm both test classes execute.
- Add a real repository-typed field or method parameter to
OrderController. Run again; the new architecture test must fail and name the forbidden dependency. - Remove that reference and run again. The test must pass.
- Repeat the failure check with a class you create in
com.example.orders.web.admin. Your rule should cover it too. Remove the experimental class and runmvn clean test.
A plain unused import statement is not a reliable negative example: it can leave
no dependency in compiled bytecode. A field or parameter type makes the intended dependency explicit.
One reference may produce multiple reported details.
Syntax hint
Keep the supplied test's import statements and package declaration nearby. Change the rule's source package and target package to express the new requirement. Choose a test name that states the starting condition and expected result. Do not disable the empty-selection check.
My rule passes after adding the forbidden field
Check that Maven discovered your *Test class, that the method has Jupiter's
@Test, and that you called check(classes). Then verify the imported
root and package spellings. Run mvn clean test to rebuild the bytecode.
Explain the result
Before opening lesson 2, explain why the controller can use a service that uses a repository while your direct-dependency rule still passes. Also explain why a repository-typed field can violate the rule even when no request ever uses that field.