Skip to content

Instantly share code, notes, and snippets.

@up1
Last active May 12, 2026 17:12
Show Gist options
  • Select an option

  • Save up1/9f9c96ea436aa60111c47da051eab960 to your computer and use it in GitHub Desktop.

Select an option

Save up1/9f9c96ea436aa60111c47da051eab960 to your computer and use it in GitHub Desktop.
ArchUnit for Spring Boot
@AnalyzeClasses(packages = "com.example.demoarch")
public class ArchitectureTest {
@ArchTest
static final ArchRule layer_dependencies = layeredArchitecture()
.consideringAllDependencies()
.layer("Controller").definedBy("..controller..")
.layer("Service").definedBy("..service..")
.layer("Repository").definedBy("..repository..")
.whereLayer("Controller").mayNotBeAccessedByAnyLayer()
.whereLayer("Service").mayOnlyBeAccessedByLayers("Controller")
.whereLayer("Repository").mayOnlyBeAccessedByLayers("Service");
@ArchTest
static final ArchRule controller_naming = classes()
.that().resideInAPackage("..controller..")
.should().haveSimpleNameEndingWith("Controller")
.andShould().beAnnotatedWith(RestController.class)
.andShould().onlyDependOnClassesThat().resideInAnyPackage("java..", "org..", "..service..");
@ArchTest
static final ArchRule service_classes = classes()
.that().resideInAPackage("..service..")
.should().haveSimpleNameEndingWith("Service")
.andShould().beAnnotatedWith(Service.class)
.andShould().onlyDependOnClassesThat().resideInAnyPackage("java..", "org..", "..repository..");
}
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>1.4.2</version>
<scope>test</scope>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment