アーキテクチャ適合性関数と境界テスト
自動化した適合性関数と依存方向のテストを使い、時間が経ってもアーキテクチャの境界を守る方法を学びます。
「アーキテクチャ適合性関数と境界テスト」はCoddyKit上の無料Clean Architecture & Design Patterns in Practiceレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはClean Architecture & Design Patterns in Practice学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Clean Architecture & Design Patterns in Practiceコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Architecture Erodes Silently
A clean design tends to decay. Under deadline pressure, someone imports the database into an entity, or a use case reaches into the web layer.
Code review alone misses these. We need automated guards.
What Is a Fitness Function?
An architectural fitness function is an automated test that asserts a structural property of the system.
Just as unit tests guard behavior, fitness functions guard architecture — for example, the direction of dependencies.
The Rule We Want to Enforce
In Clean Architecture, dependencies point inward:
- Entities depend on nothing.
- Use cases depend only on entities.
- Frameworks depend inward, never the reverse.
A fitness function can fail the build if this is broken.
Expressing It as a Test
Tools like ArchUnit let you encode the rule directly.
ArchRule rule = classes()
.that().resideInAPackage("..domain..")
.should().onlyDependOnClassesThat()
.resideInAnyPackage("..domain..", "java..");Forbidding Forbidden Imports
You can also assert that the core never touches infrastructure.
noClasses()
.that().resideInAPackage("..usecase..")
.should().dependOnClassesThat()
.resideInAPackage("..web..");Boundary Contract Tests
Beyond dependency direction, test that adapters honor their port contracts.
Run the same suite against every implementation of a gateway so a new adapter cannot silently break the boundary.
A Simple Home-Grown Check
Even without a library you can scan for violations programmatically.
public class Main {
public static void main(String[] a){
String[] domainImports = {"java.util.List", "domain.Order"};
boolean clean = true;
for (String imp : domainImports) {
if (imp.startsWith("web.") || imp.startsWith("db.")) { clean = false; }
}
System.out.println("Domain layer clean: " + clean);
}
}Running Them in CI
Fitness functions belong in the continuous integration pipeline.
When a pull request breaks a boundary, the build goes red immediately — long before the violation spreads through the codebase.
Choosing the Right Functions
- Layer dependency direction.
- No framework imports in the core.
- Naming and package conventions.
- Cyclic-dependency detection.
Start with the few rules that matter most for your design.
Evolving the Rules
Fitness functions are living. As the architecture intentionally evolves, update the rules to match the new intent.
A failing fitness function is a prompt to decide: fix the code, or consciously change the rule.
Balancing Strictness
Too many brittle rules cause friction and get disabled. Too few let decay creep in.
Aim for a small set of high-value, stable rules that protect the boundaries you care most about.
Quick Check
Test your understanding of fitness functions.
Recap
You learned to defend boundaries over time.
- Fitness functions automate architectural rules.
- Enforce inward dependency direction and a framework-free core.
- Run them in CI and evolve them deliberately as the design changes.
よくある質問
「アーキテクチャ適合性関数と境界テスト」レッスンは無料ですか?
はい。「アーキテクチャ適合性関数と境界テスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Clean Architecture & Design Patterns in Practiceコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Clean Architecture & Design Patterns in Practiceコースには全4レッスンが含まれています。
「アーキテクチャ適合性関数と境界テスト」で何を学びますか?
自動化した適合性関数と依存方向のテストを使い、時間が経ってもアーキテクチャの境界を守る方法を学びます。 ブラウザで直接実行するハンズオンコードでClean Architecture & Design Patterns in Practiceを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Clean Architecture & Design Patterns in Practiceを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのClean Architecture & Design Patterns in Practiceは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「アーキテクチャ適合性関数と境界テスト」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このClean Architecture & Design Patterns in Practiceレッスンでコードを書いて実行できますか?
はい。すべてのClean Architecture & Design Patterns in Practiceレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 層別テスト戦略
- Clean Archのデプロイに関する考慮事項
- Cleanなシステムの発展と保守
- アーキテクチャ適合性関数と境界テスト