Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsJul 19, 2026·3 min de lecture

ArchUnit — Architecture Testing Library for Java

ArchUnit lets you write unit tests that enforce architecture rules in plain Java, catching layering violations, cyclic dependencies, and naming conventions at build time.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
ArchUnit Overview
Commande d'installation directe
npx -y tokrepo@latest install 08b4f003-83b2-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

Introduction

ArchUnit is a Java library for checking the architecture of your codebase through ordinary unit tests. It analyzes compiled bytecode to verify layering constraints, dependency rules, naming conventions, and more. Because tests run inside JUnit or TestNG, architecture violations surface alongside other test failures in your existing CI pipeline.

What ArchUnit Does

  • Validates package dependency rules to enforce layered or hexagonal architectures
  • Detects cyclic dependencies between packages or classes
  • Enforces naming conventions such as requiring classes in a service package to end in Service
  • Checks annotation usage patterns like ensuring all REST controllers carry specific annotations
  • Integrates with JUnit 4, JUnit 5, and TestNG with zero additional infrastructure

Architecture Overview

ArchUnit imports Java classes from the classpath using an ASM-based bytecode reader, building an in-memory model of classes, methods, fields, and their relationships. The fluent rule API (classes().that()...should()...) compiles these constraints into predicates that are evaluated against the imported model. Custom rules can operate on the same model using the core API. The library caches class imports across tests in the same suite for performance.

Self-Hosting & Configuration

  • Add archunit-junit5 (or archunit-junit4) as a test dependency in Maven or Gradle
  • Annotate test classes with @AnalyzeClasses(packages = "com.yourapp")
  • Write architecture rules using the fluent DSL and annotate them with @ArchTest
  • Use the predefined Architectures.layeredArchitecture() for standard layer checks
  • Run tests via mvn test or ./gradlew test — no additional tooling required

Key Features

  • Plain Java rules — no external DSL, config files, or plugins needed
  • Predefined rules for layered architecture, onion architecture, and slice isolation
  • Extensible core API for writing custom checks on the class graph
  • Freezing feature stores known violations in a baseline, letting teams fix issues incrementally
  • Fast execution — bytecode analysis avoids starting the full application context

Comparison with Similar Tools

  • Checkstyle/PMD — Focus on code style and simple bug patterns; ArchUnit validates structural architecture rules
  • SonarQube — Server-based static analysis; ArchUnit runs inside unit tests with no external service
  • jDepend — Measures package coupling metrics; ArchUnit lets you write expressive pass/fail rules
  • Dependency-Check — Scans for vulnerable libraries; ArchUnit checks internal code structure
  • Spring Modulith — Spring-specific module testing; ArchUnit is framework-agnostic

FAQ

Q: Does ArchUnit require a running application? A: No. ArchUnit analyzes compiled bytecode directly from the classpath without starting a Spring context or application server.

Q: How do I handle existing violations I cannot fix immediately? A: Use the freezing feature: FreezingArchRule.freeze(rule). It stores current violations in a file and only fails on new ones.

Q: Can ArchUnit check Kotlin or Scala code? A: Yes. Since ArchUnit analyzes JVM bytecode, it works with any language that compiles to class files.

Q: Does ArchUnit slow down the test suite? A: Class import is typically fast (seconds for medium codebases) and cached across tests, adding minimal overhead.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires