{"slug":"clean-architecture-testing","title":"clean-architecture-testing","summary":"Use when it is unclear where a test belongs and what it is allowed to talk to — which test project and layer it goes in, whether it gets a real database, an in-memory fake, or no double at all, and whether a value object or a constructor is worth a test of its own. Also use when ","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-11-29T00:42:29.675096Z","repo":{"url":"https://github.com/SebastienDegodez/skraft-plugin","stars":17,"forks":3,"license":"GPL-3.0","updatedAt":"2026-11-29T00:18:21Z"},"bodyHtml":"<hr>\n<h2>name: clean-architecture-testing\ndescription: Use when it is unclear where a test belongs and what it is allowed to talk to — which test project and layer it goes in, whether it gets a real database, an in-memory fake, or no double at all, and whether a value object or a constructor is worth a test of its own. Also use when covering an adapter that calls an external API, a database or the file system, and when layer dependencies must be enforced by the build instead of by review.</h2>\n<h1>Clean Architecture Testing</h1>\n<h2>Overview</h2>\n<p>Defines <strong>where</strong> to test and <strong>with which doubles</strong> in a Clean Architecture solution. Language-agnostic. Does NOT redefine the TDD cycle — that is owned by <code>outside-in-tdd</code>.</p>\n<p><strong>Core principle:</strong> every test enters through <strong>one boundary</strong> and asserts at the next boundary. Choice of double follows the layer, not convenience.</p>\n<p><strong>REQUIRED SUB-SKILL:</strong> Use <code>outside-in-tdd</code> for the RED → SYNTHESIZE-GREEN → COMMIT cycle. This skill only routes you to the correct layer and doubles.</p>\n<h2>The Iron Rule for Domain Tests</h2>\n<p><strong>Default = Application-level acceptance test.</strong> Do NOT write Domain tests by default.</p>\n<p>Extract a Domain test ONLY when a business rule:</p>\n<ul>\n<li>Has complex invariants or a large edge-case matrix, AND</li>\n<li>Is extracted into a reusable Policy / Domain Service / Specification.</li>\n</ul>\n<p><strong>NEVER test directly:</strong></p>\n<ul>\n<li>Constructors (unless they enforce complex invariants)</li>\n<li>Simple value objects (covered by usage in Application acceptance tests)</li>\n<li>Getters / setters / DTOs / ViewModels / passive data structures</li>\n</ul>\n<p>A test asserting <code>new PolicyNumber(\"abc\").value == \"abc\"</code> is noise. It tests the compiler, not behavior.</p>\n<h2>Test Project Organization</h2>\n<p><strong>Rule: exactly two test projects per bounded context</strong>, regardless of language.</p>\n<table>\n<thead>\n<tr>\n<th>Test project</th>\n<th>Targets</th>\n<th>Contains</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>&lt;Context&gt;.UnitTest</code></td>\n<td>Domain + Application</td>\n<td>Domain unit tests (rare, extracted rules) + Application acceptance tests (mocks on output gateways, in-memory fakes)</td>\n</tr>\n<tr>\n<td><code>&lt;Context&gt;.IntegrationTest</code></td>\n<td>Infrastructure + API + Architecture</td>\n<td>Infrastructure integration tests (real containers), API end-to-end tests (in-process app host), architecture tests</td>\n</tr>\n</tbody>\n</table>\n<h3>Per-language mapping</h3>\n<table>\n<thead>\n<tr>\n<th>Ecosystem</th>\n<th><code>UnitTest</code> project</th>\n<th><code>IntegrationTest</code> project</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>.NET</td>\n<td><code>tests/&lt;Context&gt;.UnitTest/</code> csproj</td>\n<td><code>tests/&lt;Context&gt;.IntegrationTest/</code> csproj</td>\n</tr>\n<tr>\n<td>Java (Maven / Gradle)</td>\n<td><code>&lt;context&gt;-unit-test</code> module</td>\n<td><code>&lt;context&gt;-integration-test</code> module</td>\n</tr>\n<tr>\n<td>Python</td>\n<td><code>tests/unit/</code> package</td>\n<td><code>tests/integration/</code> package</td>\n</tr>\n<tr>\n<td>React front end (Vitest)</td>\n<td><code>tests/unit/</code> folder</td>\n<td><code>tests/integration/</code> folder</td>\n</tr>\n<tr>\n<td>TypeScript / Node</td>\n<td><code>test/unit/</code> workspace</td>\n<td><code>test/integration/</code> workspace</td>\n</tr>\n</tbody>\n</table>\n<h3>Multi-context solutions</h3>\n<p>One pair per bounded context. Shared helpers (fakes, builders) live in a separate non-test project (<code>TestKit</code> / <code>tests-shared</code>) referenced by both.</p>\n<h3>Test folder naming</h3>\n<p>Test class folders and namespaces are named after the <strong>feature or behavior</strong> under test — never\nafter the GitHub issue, story ID, or sprint.</p>\n<table>\n<thead>\n<tr>\n<th>Wrong</th>\n<th>Right</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>EligibilityCheck/STORY-42/</code></td>\n<td><code>EligibilityCheck/</code></td>\n</tr>\n<tr>\n<td><code>tests/OrderSubmission/issue-105/</code></td>\n<td><code>tests/OrderSubmission/</code></td>\n</tr>\n<tr>\n<td><code>PlaceOrder/Sprint3/</code></td>\n<td><code>PlaceOrder/</code></td>\n</tr>\n</tbody>\n</table>\n<p>One folder per feature or bounded behavior. The folder name answers \"what does this test?\"\nnot \"which ticket created it?\"</p>\n<h3>Forbidden combinations</h3>\n<table>\n<thead>\n<tr>\n<th>Wrong</th>\n<th>Why</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Single test project mixing unit + integration</td>\n<td>Slow tests pollute the fast loop; developers start skipping the suite</td>\n</tr>\n<tr>\n<td>Splitting by production layer (<code>*.Domain.Test</code>, <code>*.Application.Test</code>, …)</td>\n<td>Explodes project count, fights the boundary policy</td>\n</tr>\n<tr>\n<td>End-to-end app host test in <code>UnitTest</code></td>\n<td>API tests need full composition — belong in <code>IntegrationTest</code></td>\n</tr>\n<tr>\n<td>Real container (DB / broker) in <code>UnitTest</code></td>\n<td>Breaks the &lt;1 s suite promise</td>\n</tr>\n<tr>\n<td>Architecture scanner test in <code>UnitTest</code></td>\n<td>Architecture tests are a CI gate, not a development loop</td>\n</tr>\n<tr>\n<td>Third test project outside <code>UnitTest</code> / <code>IntegrationTest</code></td>\n<td>Creates a blind spot outside the two-speed suite; all tests belong to one of the two defined projects</td>\n</tr>\n</tbody>\n</table>\n<h2>Testing Strategy per Layer</h2>\n<table>\n<thead>\n<tr>\n<th>Layer</th>\n<th>Test type</th>\n<th>Entry point</th>\n<th>Doubles</th>\n<th>Target speed</th>\n<th>When to write</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><strong>SharedKernel</strong></td>\n<td>None</td>\n<td>—</td>\n<td>—</td>\n<td>—</td>\n<td>Never (interfaces + base classes, no logic)</td>\n</tr>\n<tr>\n<td><strong>Domain</strong></td>\n<td>Pure unit (rare)</td>\n<td>Policy / Domain Service / Specification</td>\n<td>None (real objects)</td>\n<td>&lt;10 ms</td>\n<td>Only for extracted complex rules</td>\n</tr>\n<tr>\n<td><strong>Application</strong></td>\n<td>Acceptance (sociable)</td>\n<td>Command / Query handler or UseCase</td>\n<td>Mocks on output gateways, hand-written in-memory fakes for stateful reuse</td>\n<td>&lt;100 ms</td>\n<td><strong>Default</strong> — one per Gherkin scenario</td>\n</tr>\n<tr>\n<td><strong>Infrastructure</strong></td>\n<td>Integration</td>\n<td>Gateway adapter (repository, read service, message handler)</td>\n<td>Real I/O via containers (DB, broker); external APIs via contract mock server</td>\n<td>1-5 s</td>\n<td>One per adapter contract</td>\n</tr>\n<tr>\n<td><strong>API</strong></td>\n<td>End-to-end</td>\n<td>HTTP endpoint via in-process app host</td>\n<td>Real internal stack; contract mock server for downstream externals</td>\n<td>1-5 s</td>\n<td>Walking skeleton + one happy path per endpoint</td>\n</tr>\n<tr>\n<td><strong>Architecture</strong></td>\n<td>Static analysis</td>\n<td>Assembly / module scan</td>\n<td>None</td>\n<td>&lt;1 s</td>\n<td>One rule per constraint, CI gate</td>\n</tr>\n</tbody>\n</table>\n<h2>Doubles Policy (by role, not by library)</h2>\n<table>\n<thead>\n<tr>\n<th>Role</th>\n<th>Use for</th>\n<th>Never for</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><strong>Real domain object</strong></td>\n<td>Always, at every layer that touches Domain</td>\n<td>—</td>\n</tr>\n<tr>\n<td><strong>Mock / stub on output gateway</strong></td>\n<td>Output gateways at Application level (repository, dispatcher, external service interfaces)</td>\n<td>Domain objects, Application handlers</td>\n</tr>\n<tr>\n<td><strong>Hand-written in-memory fake</strong></td>\n<td>Reusable across many Application tests, stateful scenarios</td>\n<td>One-off single-test cases</td>\n</tr>\n<tr>\n<td><strong>Real container</strong> (DB, broker)</td>\n<td>Infrastructure adapter tests exclusively</td>\n<td>Application or Domain tests</td>\n</tr>\n<tr>\n<td><strong>Contract mock server</strong> (for external APIs / brokers)</td>\n<td>Infrastructure adapter tests against externals; API E2E with downstream externals</td>\n<td>Internal domain logic, Application handlers</td>\n</tr>\n<tr>\n<td><strong>In-process app host</strong> (boots the full API)</td>\n<td>API layer end-to-end tests exclusively</td>\n<td>Application, Domain, Infrastructure</td>\n</tr>\n<tr>\n<td><strong>Architecture scanner</strong></td>\n<td>Architecture tests</td>\n<td>Any behavioral test</td>\n</tr>\n</tbody>\n</table>\n<p>Pick one mocking library per solution and stick to it. Same for the app host factory. Mixing libraries across tests is a smell.</p>\n<h2>Per-Layer Examples (pseudo-code)</h2>\n<p>Full runnable examples: <a href=\"references/examples-dotnet.md\">examples-dotnet.md</a> (.NET), <a href=\"references/examples-java.md\">examples-java.md</a> (Java / Spring Boot, Maven), <a href=\"references/examples-python.md\">examples-python.md</a> (Python, pytest) and <a href=\"references/examples-react.md\">examples-react.md</a> (React, Vitest). The roles are identical across languages.</p>\n<h3>Application — acceptance test</h3>\n<pre><code>given repository := mock of IOrderRepository\n  and dispatcher := mock of IDomainEventDispatcher\n  and handler := new PlaceOrderCommandHandler(repository, dispatcher)\n\nwhen handler.handle(PlaceOrderCommand(id, \"Alice\"))\n\nthen repository received add(order where order.customerName == \"Alice\") exactly once\n and dispatcher received dispatch(events containing OrderPlacedEvent) exactly once\n</code></pre>\n<h3>Infrastructure — integration test</h3>\n<pre><code>given db := start Postgres container\n  and ctx := connect(db.connectionString) and migrate\n  and sut := new OrderRepository(ctx)\n  and order := Order.create(id, \"Alice\")\n\nwhen sut.add(order)\n\nthen ctx.orders contains one row with customerName == \"Alice\"\n</code></pre>\n<h3>API — end-to-end test</h3>\n<pre><code>given appHost := boot in-process app host\n  and client := appHost.createHttpClient()\n\nwhen response := client.POST(\"/orders\", { customerName: \"Alice\" })\n\nthen response.status == 201\n and response.location is not null\n</code></pre>\n<h3>Architecture — layer rule</h3>\n<pre><code>assert each project references only its inner neighbour\n  (Api → Infrastructure → Application → Domain)\n\nassert types in Domain depend only on Domain and the language core\nassert types in Application depend only on Application, Domain and the language core\n  else fail with \"Violations: ...\"\n</code></pre>\n<p>The language core excludes I/O, network and persistence. Allow-list the business code: a rule that only forbids the other layers lets every framework through.</p>\n<h2>External Contract Mocks (Infrastructure &amp; API)</h2>\n<p>When an adapter (Infrastructure) or endpoint (API) talks to an external HTTP / gRPC / async API:</p>\n<ul>\n<li>Start a <strong>contract mock server</strong> from the external's published contract (OpenAPI, gRPC proto, AsyncAPI).</li>\n<li>Point the adapter's client at the mock's URL / topic.</li>\n<li>In API end-to-end tests, wire downstream externals to the mock through DI overrides.</li>\n<li>The same contract file can later validate real responses (contract verification).</li>\n</ul>\n<p>Out of scope for this skill — see <code>api-contract-testing</code> <em>(upcoming)</em> for the full pattern.</p>\n<h2>Decision Tree — Which Test Layer?</h2>\n<pre><code>digraph which_layer {\n    node [shape=box];\n    change [shape=diamond, label=\"What is the change?\"];\n    usecase [label=\"Use case orchestration\\n(load/save/publish)\"];\n    gateway [label=\"Gateway adapter\\n(DB, HTTP, broker)\"];\n    http [label=\"HTTP endpoint wiring\"];\n    rule [label=\"Complex reusable rule\\n(many edge cases)\"];\n    boundary [label=\"Layer boundary rule\"];\n    app_test [style=filled, label=\"Application acceptance test\\n(mocks on output gateways)\"];\n    infra_test [style=filled, label=\"Infrastructure integration test\\n(real container / contract mock)\"];\n    api_test [style=filled, label=\"API end-to-end test\\n(in-process app host)\"];\n    domain_test [style=filled, label=\"Domain unit test\\n(pure)\"];\n    arch_test [style=filled, label=\"Architecture test\\n(static scan)\"];\n\n    change -&gt; usecase -&gt; app_test;\n    change -&gt; gateway -&gt; infra_test;\n    change -&gt; http -&gt; api_test;\n    change -&gt; rule -&gt; domain_test;\n    change -&gt; boundary -&gt; arch_test;\n}\n</code></pre>\n<p>Extended tree with tie-breakers: <a href=\"references/doubles-decision-tree.md\">doubles-decision-tree.md</a>.</p>\n<h2>Anti-Patterns</h2>\n<table>\n<thead>\n<tr>\n<th>Anti-pattern</th>\n<th>Fix</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Testing <code>new ValueObject(x).value == x</code></td>\n<td>Delete. Covered by usage in Application test.</td>\n</tr>\n<tr>\n<td>Mocking a domain object</td>\n<td>Use the real aggregate. Mock only output gateways.</td>\n</tr>\n<tr>\n<td>Real container in an Application-level test</td>\n<td>Move to Infrastructure layer or use an in-memory fake.</td>\n</tr>\n<tr>\n<td>Shared global container across test classes</td>\n<td>One container per test class, isolated lifecycle.</td>\n</tr>\n<tr>\n<td>In-process app host used to test a handler</td>\n<td>Switch to an Application-level test with mocks.</td>\n</tr>\n<tr>\n<td>No architecture test guarding layer references</td>\n<td>Add the rule. Layer discipline must be enforced by CI, not reviews.</td>\n</tr>\n<tr>\n<td>Architecture test that only forbids the other layers</td>\n<td>Allow-list what Domain and Application may depend on; a framework or an I/O client passes a layer-only rule.</td>\n</tr>\n<tr>\n<td>In-memory fake DB provider used as Infrastructure test</td>\n<td>Infrastructure tests MUST use the real provider via real container. In-memory providers silently accept invalid SQL.</td>\n</tr>\n<tr>\n<td>One acceptance test covers 5 rules</td>\n<td>Split: one Gherkin scenario = one test.</td>\n</tr>\n<tr>\n<td>Duplicating acceptance coverage with a Domain test on the same rule</td>\n<td>Delete the Domain test unless the rule was extracted.</td>\n</tr>\n</tbody>\n</table>\n<h2>Red Flags — STOP</h2>\n<p>These thoughts signal you're about to violate the policy:</p>\n<ul>\n<li>\"I'll add a quick test on this constructor for safety.\"</li>\n<li>\"Let me mock this aggregate to simplify the test.\"</li>\n<li>\"I need an in-memory DB provider here because real containers are slow.\"</li>\n<li>\"This handler test needs a real DB to verify my query.\"</li>\n<li>\"Architecture tests slow down the build — I'll skip them.\"</li>\n<li>\"A Domain test per value object will bump coverage.\"</li>\n</ul>\n<p><strong>All of these mean: stop and re-read the table.</strong> Coverage is not the goal. Behavior at the correct boundary is.</p>\n<h2>References</h2>\n<ul>\n<li><a href=\"references/examples-dotnet.md\">examples-dotnet.md</a> — full runnable .NET examples (FakeItEasy, Testcontainers, in-process app host, architecture scanner)</li>\n<li><a href=\"references/examples-java.md\">examples-java.md</a> — full runnable Java / Spring Boot examples (JUnit 5, Mockito, Testcontainers, ArchUnit)</li>\n<li><a href=\"references/examples-python.md\">examples-python.md</a> — runnable Python examples (pytest, hand-written fakes, testcontainers, FastAPI <code>TestClient</code>, import-linter)</li>\n<li><a href=\"references/examples-react.md\">examples-react.md</a> — runnable React examples (Vitest, in-memory gateways, Testing Library, MSW, the ESLint guard)</li>\n<li><a href=\"references/architecture-rules.md\">architecture-rules.md</a> — complete architecture rule set (project references + allow-list; .NET implementation)</li>\n<li><a href=\"references/doubles-decision-tree.md\">doubles-decision-tree.md</a> — extended decision tree with tie-breakers</li>\n</ul>\n","files":[{"path":"references/architecture-rules.md","sizeBytes":8229,"isText":true},{"path":"references/doubles-decision-tree.md","sizeBytes":4771,"isText":true},{"path":"references/examples-dotnet.md","sizeBytes":9470,"isText":true},{"path":"references/examples-java.md","sizeBytes":9593,"isText":true},{"path":"references/examples-python.md","sizeBytes":3743,"isText":true},{"path":"references/examples-react.md","sizeBytes":4988,"isText":true},{"path":"SKILL.md","sizeBytes":12534,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-11-29T00:43:51.77386Z","sha256":"75E1C1C30435E52DA91900E0E8001B7D1C4216BA1430B16DE4D1E6491E1CF1C8","sizeBytes":21105},"review":null,"source":{"repositoryUrl":"https://github.com/SebastienDegodez/skraft-plugin","path":"plugins/skraft-framework/skills/clean-architecture-testing","license":"GPL-3.0","commit":"c6117c6236a25490bf1916b131866069740c9b22","subtreeSha":"84E2965A763CE51D3B3E7E45B02A2ECEA44920EC1521FBF5D2AE25751DAF87A2","lastSyncedAt":"2026-11-29T00:42:27.348488Z"},"reviewedAt":"2026-11-29T00:46:55.809273Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/SebastienDegodez/skraft-plugin/tree/main/plugins/skraft-framework/skills/clean-architecture-testing"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install sebastiendegodez-skraft-plugin@llmmart"},{"target":"git","command":"git clone https://github.com/SebastienDegodez/skraft-plugin.git"}]}