Scripts2026年7月19日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
ArchUnit Overview
直接安装命令
npx -y tokrepo@latest install 08b4f003-83b2-11f1-9bc6-00163e2b0d79 --target codex

先 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产