{"skill":{"slug":"bookforge-test-harness-entry-diagnostics","displayName":"Test Harness Entry Diagnostics","summary":"Diagnose exactly why a class or method cannot be placed under test and route to the right dependency-breaking technique. Use whenever a developer says 'I can...","description":"---\nname: test-harness-entry-diagnostics\ndescription: \"Diagnose exactly why a class or method cannot be placed under test and route to the right dependency-breaking technique. Use whenever a developer says 'I can't instantiate this class in tests', 'the test harness won't compile', 'this class has too many constructor dependencies', 'the constructor connects to the database', 'can't test this private method', 'I need to sense what this method does', 'hidden singleton dependency'. Activates for 'test harness', 'class under test', 'constructor dependencies', 'irritating parameter', 'hidden dependency', 'construction blob', 'pass null', 'construction test', 'method not accessible', 'method side effects', 'can't get this class in a test harness', 'can't run this method in a test harness', 'singleton in constructor', 'include dependencies', 'onion parameter', 'aliased parameter'.\"\nversion: 1.0.0\nhomepage: https://github.com/bookforge-ai/bookforge-skills/tree/main/books/working-effectively-with-legacy-code/skills/test-harness-entry-diagnostics\nmetadata: {\"openclaw\":{\"emoji\":\"📚\",\"homepage\":\"https://github.com/bookforge-ai/bookforge-skills\"}}\nstatus: draft\nsource-books:\n  - id: working-effectively-with-legacy-code\n    title: \"Working Effectively with Legacy Code\"\n    authors: [\"Michael C. Feathers\"]\n    chapters: [9, 10]\ndomain: software-engineering\ntags: [legacy-code, testing, refactoring, software-engineering, dependency-injection]\ndepends-on:\n  - legacy-code-change-algorithm\n  - seam-type-selector\nexecution:\n  tier: 2\n  mode: hybrid\n  inputs:\n    - type: codebase\n      description: \"Class file + constructor/method signature + test harness error message or observed obstacle\"\n  tools-required: [Read, Grep, Bash]\n  tools-optional: [Edit]\n  mcps-required: []\n  environment: \"Codebase with a test framework (any xUnit family). Access to the class under investigation.\"\ndiscovery:\n  goal: \"Pinpoint the exact reason a class or method resists testing and produce a technique recommendation with ordered sequence.\"\n  tasks:\n    - \"Run a construction test to surface constructor obstacles\"\n    - \"Classify the obstacle via 4 root causes and 7 class-level cases\"\n    - \"If method-level, classify via 4 Chapter 10 obstacles\"\n    - \"Route to the correct dependency-breaking technique with rationale\"\n    - \"Produce a diagnostic artifact for handoff to technique execution\"\n  audience:\n    roles: [software-engineer, backend-developer, qa-engineer]\n    experience: intermediate\n  when_to_use:\n    triggers:\n      - \"Class won't instantiate in a test\"\n      - \"Method can't be invoked in a test\"\n      - \"Developer doesn't know which dependency-breaking technique to apply\"\n      - \"Test harness won't compile when the class is added\"\n    prerequisites:\n      - skill: legacy-code-change-algorithm\n        why: \"This diagnostic executes Step 3 (break dependencies) of the change algorithm — run this skill when you reach that step\"\n    not_for:\n      - \"Class IS testable but tests are slow — use unit-test-quality-checker instead\"\n      - \"Designing new code — applies only to existing, hard-to-test code\"\n  environment:\n    codebase_required: true\n    codebase_helpful: true\n    works_offline: true\n  quality:\n    scores: {with_skill: null, baseline: null, delta: null}\n    tested_at: null\n    eval_count: null\n    assertion_count: 14\n    iterations_needed: null\n---\n\n# Test Harness Entry Diagnostics\n\nA systematic diagnostic for determining exactly why a class or method cannot be placed under test, and routing each specific obstacle to the correct dependency-breaking technique. Based on Feathers' Chapter 9 (class-level) and Chapter 10 (method-level) obstacle catalogs.\n\n## When to Use\n\nUse this skill when Step 3 of the Legacy Code Change Algorithm (\"Break Dependencies\") is blocked — when a developer cannot write a test because the class won't instantiate or the method can't be invoked. The diagnostic applies whether the problem surfaces as a compile error, a runtime exception, a dangerously slow test, or an inability to sense what the code actually did.\n\nDo not use this skill when the class is already testable but the tests are too slow or fragile — that is a test quality problem, not a dependency-breaking problem.\n\n## Context and Input Gathering\n\nBefore running the diagnostic, collect:\n\n1. **Class name and language** — the class you are trying to place under test\n2. **Constructor signature** — parameter types, their constructors if visible\n3. **The error or obstacle** — compile error, runtime exception, side-effect description, or accessibility barrier\n4. **Test framework in use** — JUnit, NUnit, Google Test, etc.\n5. **What you want to test** — a specific method, a behavior, a value returned\n\nIf you have not yet attempted to instantiate the class in a test, do that first (Step 1 below). The compiler or runtime error message IS the diagnostic signal.\n\n## Process\n\n### Step 1: Write a Construction Test\n\nBefore any analysis, write the simplest possible test: one that just calls the constructor with no assertions.\n\n```java\n// Java / JUnit example\npublic void testCreate() {\n    MyClass obj = new MyClass();\n}\n```\n\n```cpp\n// C++ / CppUnit example\nTEST(create, MyClass) {\n    MyClass obj;\n}\n```\n\n**Why this first:** The compiler tells you exactly what dependencies are missing. The runtime tells you exactly which side effects fire. You do not need to reason about the code — the toolchain performs the diagnosis for you. Construction tests look strange (no assertion, no verification), but they are free diagnostic instruments. Once you can construct the object, rename or replace the test with a real one.\n\nAttempt to compile and run. The error message routes you directly to the root cause in Step 2.\n\n### Step 2: Classify by Root Cause\n\nFeathers identifies four root causes for class-level test resistance. Map your error to one:\n\n| # | Root Cause | Typical Signal |\n|---|-----------|----------------|\n| 1 | Objects cannot be created easily | Too many hard-to-build parameters; nested object chains |\n| 2 | Test harness won't build with the class in it | Compile errors from header chains (C++) or linker failures |\n| 3 | Constructor has bad side effects | Test connects to DB, sends email, writes files, hits network |\n| 4 | Significant work in constructor; need to sense it | Heavy initialization; values computed during construction are needed in assertions |\n\nRoot causes 1, 6, and 7 (Onion Parameter, Aliased Parameter) → hard-to-create objects\nRoot cause 2 → build/link failure (Horrible Include Dependencies, C++ only)\nRoot causes 3 and 4 → constructor side effects or resource allocation (Hidden Dependency, Construction Blob)\nRoot cause 4 (sensing) → Construction Blob with sensing need\n\n### Step 3: Match to the 7 Class-Level Cases (Chapter 9)\n\nOnce you have the root cause, identify the specific case using these detection rules:\n\n**Case 1 — Irritating Parameter**\n- Signal: A constructor parameter itself has a heavy constructor (network, file, DB on construction)\n- The parameter IS passed in explicitly; it's just expensive to create\n- Technique: **Extract Interface** on the problematic parameter type, then create a fake implementation. Alternative: **Pass Null** if the parameter is not actually used in the code path under test.\n- Why Extract Interface: It severs the compile-time dependency on the concrete type without changing the caller's API.\n- Why Pass Null is legitimate: In a garbage-collected language (Java, C#), passing null for an unused parameter simply means the variable is never dereferenced. The runtime will throw if you're wrong, making the mistake immediately visible.\n\n**Case 2 — Hidden Dependency**\n- Signal: Constructor calls `new SomeService()` or `SomeGlobal.connect()` internally — the dependency is not in the parameter list\n- No way to substitute the dependency from outside\n- Technique: **Parameterize Constructor** — move the `new` call outside the constructor and pass the object in. Provide a convenience constructor with the original signature that delegates to the new one.\n- Why: Externalizing the dependency gives tests control over which implementation is used.\n\n**Case 3 — Construction Blob**\n- Signal: Constructor builds a chain of objects (A creates B creates C), and you need to sense through one of them\n- Parameterize Constructor would require passing too many parameters\n- Techniques (in order of preference):\n  1. **Extract and Override Factory Method** (Java/C# only) — extract the object-creation code into a protected factory method, override it in a test subclass\n  2. **Supersede Instance Variable** — add a setter that replaces the created object after construction; use with extreme care in C++ (manual memory management)\n- Why not Parameterize Constructor here: A large parameter list creates its own construction problem.\n\n**Case 4 — Irritating Global Dependency (Singleton)**\n- Signal: Constructor calls `SomeClass.getInstance()` or accesses a static global; cannot be replaced from outside\n- Technique: **Introduce Static Setter** — add a `setTestingInstance(T instance)` method to the singleton that replaces the static field. In tests, set a fake before each test and reset after.\n- Supplement with: **Extract Interface** on the singleton so the static field holds an interface type, not a concrete class — this allows fake implementations.\n- Why: Relaxing the singleton property in test environments is safe as long as no production code calls the setter. A build-time check can enforce this.\n\n**Case 5 — Horrible Include Dependencies (C++ only)**\n- Signal: Including the class header transitively pulls in thousands of lines; the test file takes minutes to compile or fails to link\n- Technique: **Definition Completion** — provide stub definitions of the problematic classes in the test file or a `Fakes.h` include file, creating a separate test binary for this class\n- Why: This severs the compile-time dependency without modifying production headers. Maintenance cost is real — reserve for severe cases only.\n\n**Case 6 — Onion Parameter**\n- Signal: The constructor needs Object A, which needs Object B, which needs Object C — a deeply nested creation chain\n- Technique: **Extract Interface on the outermost layer** to create a single fake that stands in for the entire chain. If the parameter is genuinely unused in the test path, **Pass Null**.\n- Why Extract Interface on outermost: You only need to go one level deep. A fake of the immediate parameter type breaks the whole chain.\n\n**Case 7 — Aliased Parameter**\n- Signal: A parameter type inherits from a base class that is also used as a field type — you cannot extract an interface for it without redesigning the entire hierarchy\n- Technique: **Subclass and Override Method** — subclass the parameter type and override the specific method that causes the problem (the side effect or heavy operation)\n- Why not Extract Interface: Building an interface hierarchy parallel to an existing class hierarchy is disproportionate work. Selective method overriding is surgical.\n\n**Full case-to-technique table:** See `references/class-level-cases.md`.\n\n### Step 4: Apply the 4 Method-Level Obstacles (Chapter 10)\n\nIf the class IS instantiable but a specific method cannot be exercised in a test, move to Chapter 10 diagnostics. These apply after Step 3 resolves construction obstacles.\n\n**Obstacle 1 — Method Not Accessible**\n- Signal: Method is `private`, `package-private`, or otherwise hidden; you cannot call it from the test file\n- First ask: Can you test the behavior through a public method that calls this one? If yes, do that — it tests the method as it is actually used.\n- If not: Change `private` to `protected` and create a test subclass that exposes it publicly. This is preferable to reflection-based access, which masks design problems.\n- Root cause: The class has too many responsibilities. The inaccessible method belongs on a separate class. Schedule that refactoring; use the subclass approach as a bridge.\n\n**Obstacle 2 — Hard to Construct Parameters**\n- Signal: The method signature takes parameters that are expensive or impossible to construct in a test\n- This is structurally the same as class-level Cases 1, 6, and 7\n- Apply the same techniques: **Extract Interface**, **Pass Null**, or **Adapt Parameter** for sealed/final library types that cannot be subclassed\n\n**Obstacle 3 — Method Has Bad Side Effects**\n- Signal: Calling the method sends email, writes to DB, launches a process, modifies shared state\n- Technique: **Extract and Override Call** — extract the side-effecting call into its own method, then override it in a test subclass to do nothing (or capture the call for assertion)\n- Why: This lets you test the logic surrounding the side effect without triggering the side effect itself.\n\n**Obstacle 4 — Need to Sense Effects Through an Object**\n- Signal: The method produces its result by mutating an object it holds internally — there is no return value and no output parameter to check\n- Technique: **Subclass and Override Method** — override the method that performs the mutation, capture the call, and assert on captured values. Alternatively, extract the collaboration into a seam and substitute a sensing fake.\n- Why: Command/Query Separation is violated here by design. The sensing fake enforces it for the test.\n\n**Full obstacle-to-technique table:** See `references/method-level-cases.md`.\n\n### Step 5: Produce a Diagnostic Artifact\n\nAfter completing the classification, write a short diagnostic report (template in Outputs section) naming:\n- The root cause category\n- The specific case or obstacle\n- The recommended technique\n- The ordered sequence if multiple obstacles coexist\n\nWhen multiple obstacles exist, resolve them in this order:\n1. Build/compile obstacles first (Case 5 — cannot proceed until it compiles)\n2. Construction obstacles second (Cases 1–4, 6–7 — cannot write any test until the object can be created)\n3. Method-level obstacles third (Obstacles 1–4 — apply after the class is instantiable)\n\n## Inputs\n\n| Input | Required | Description |\n|-------|----------|-------------|\n| Class name | Yes | Fully qualified name of the class under investigation |\n| Language | Yes | Java, C#, C++, Python, etc. |\n| Constructor signature | Yes | Parameter types and their dependencies |\n| Error message | Recommended | Compile error, linker error, or runtime exception from the construction test |\n| Test framework | Yes | JUnit, NUnit, Google Test, pytest, etc. |\n| Target method | If method-level | The specific method you want to test |\n\n## Outputs\n\n### Diagnostic Report (`diagnostic-report.md`)\n\n```markdown\n## Test Harness Diagnostic: [ClassName]\n\n**Language:** [Java / C# / C++ / other]\n**Framework:** [JUnit / NUnit / Google Test / other]\n**Date:** [ISO date]\n\n### Construction Test Result\n[Paste compile error or runtime exception, or \"compiled and ran cleanly\"]\n\n### Root Cause\n[One of: Objects can't be created easily / Harness won't build / Constructor has bad side effects / Significant work in constructor]\n\n### Specific Case\n[One of the 7 class-level cases or 4 method-level obstacles — use the case name]\n\n### Detection Evidence\n[The specific line/error/behavior that triggered this classification]\n\n### Recommended Technique\n[Technique name (Part III reference page if known)]\n\n### Ordered Sequence (if multiple obstacles)\n1. [First technique — addresses build/compile blocker]\n2. [Second technique — addresses construction blocker]\n3. [Third technique — addresses method-level blocker]\n\n### Rationale\n[One sentence per technique explaining why it fits this specific situation]\n```\n\n## Key Principles\n\n**Construction tests are free diagnostic tools — use them first.** Writing a `testCreate()` with no assertion costs almost nothing and eliminates all guesswork about which dependencies are actually needed.\n\n**The error message IS the diagnosis — read it.** The compiler or runtime enumerates missing dependencies precisely. Treat it as a structured input to the classification process, not as noise to dismiss.\n\n**Pass Null is legitimate when the parameter is unused in the test path.** In garbage-collected languages, null for an unused parameter is safe — the runtime will surface misuse immediately as a NullPointerException. Do not pass null in production code. In C/C++, do not pass null unless the runtime detects null pointer dereferences.\n\n**When multiple obstacles exist, order matters.** Fix construction obstacles before method-level obstacles. Fix compile/link obstacles before construction obstacles. Attempting to sense through a method while the class still won't compile is wasted effort.\n\n**The goal is testability, not elegance.** Test subclasses and methods with poor names are acceptable intermediate states. Once tests exist, the design can be improved safely.\n\n## Examples\n\n### Example 1: Irritating Parameter\n\n**Situation:** `CreditValidator(RGHConnection connection, CreditMaster master, String id)` — `RGHConnection` opens a TCP socket when constructed.\n\n**Construction test result:** Compiles, but the test takes 10 seconds and fails when the server is down.\n\n**Classification:** Root Cause 1 (objects can't be created easily) → Case 1 (Irritating Parameter)\n\n**Recommended technique:** Extract Interface on `RGHConnection` → create `IRGHConnection`. Implement `FakeConnection` returning pre-configured reports. `CreditMaster` loads a file quickly — it is not the problem.\n\n**Pass Null alternative:** If the specific method being tested does not call any `RGHConnection` methods, pass `null` for `connection` and confirm with the construction test.\n\n---\n\n### Example 2: Hidden Dependency\n\n**Situation:** `PaymentProcessor()` constructor calls `new DatabaseFactory(config).connect()` internally. No parameters.\n\n**Construction test result:** Runs, but hits the database, which is unavailable in CI.\n\n**Classification:** Root Cause 3 (constructor has bad side effects) → Case 2 (Hidden Dependency)\n\n**Recommended technique:** Parameterize Constructor — extract the `DatabaseFactory` creation out of the constructor. Add `PaymentProcessor(DatabaseFactory factory)` as the primary constructor. Provide the original no-arg constructor delegating to it: `PaymentProcessor() { this(new DatabaseFactory(config)); }`. In tests, pass a fake `DatabaseFactory`.\n\n---\n\n### Example 3: Construction Blob\n\n**Situation:** `WatercolorPane(Form border, WashBrush brush, Pattern backdrop)` constructor creates `Panel`, `Panel`, and `FocusWidget` internally in sequence. You need to assert on the state of `FocusWidget` after construction.\n\n**Classification:** Root Cause 4 (significant work in constructor, needs sensing) → Case 3 (Construction Blob)\n\n**Recommended technique (Java/C#):** Extract and Override Factory Method — extract the `FocusWidget` creation into `protected FocusWidget createCursor(WashBrush brush, Panel panel)`, override it in a test subclass to return a `TestingFocusWidget`.\n\n**Fallback (C++):** Supersede Instance Variable — add `void supersedeCursor(FocusWidget* newCursor)`, call it in the test after construction. Delete the old cursor carefully.\n\n## References\n\n- `references/class-level-cases.md` — Full 7-case reference table: detection rules, techniques, worked examples, trade-offs\n- `references/method-level-cases.md` — Full 4-obstacle reference table: detection rules, techniques, worked examples\n\n**Source chapters:**\n- Chapter 9 — \"I Can't Get This Class into a Test Harness\" (class-level obstacles and 7 cases)\n- Chapter 10 — \"I Can't Run This Method in a Test Harness\" (method-level obstacles and case studies)\n\n**Part III techniques referenced:**\n- Extract Interface (p. 362)\n- Parameterize Constructor (p. 379)\n- Extract and Override Factory Method (p. 350)\n- Supersede Instance Variable (p. 404)\n- Introduce Static Setter (p. 372)\n- Definition Completion (C++ only)\n- Subclass and Override Method (p. 401)\n- Adapt Parameter (p. 326)\n- Extract and Override Call (p. 348)\n- Expose Static Method (p. 345)\n\n## License\n\n[CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) — BookForge Skills. Source: *Working Effectively with Legacy Code* by Michael C. Feathers (2004, Prentice Hall). Skills represent transformative synthesis; book content is not reproduced verbatim.\n\n## Related BookForge Skills\n\n**Prerequisites (run before this skill):**\n- `legacy-code-change-algorithm` — this diagnostic executes Step 3 of that algorithm\n- `seam-type-selector` — identifies the seam type (object, link, preprocessing) to exploit; use when the technique recommendation requires seam selection\n\n**Downstream (run after this skill):**\n- `dependency-breaking-technique-executor` — executes the specific technique identified by this diagnostic\n","tags":{"bookforge":"1.0.0","dependency-injection":"1.0.0","latest":"1.0.0","legacy-code":"1.0.0","refactoring":"1.0.0","software-engineering":"1.0.0","testing":"1.0.0"},"stats":{"comments":0,"downloads":335,"installsAllTime":0,"installsCurrent":0,"stars":0,"versions":1},"createdAt":1777972964715,"updatedAt":1778492850251},"latestVersion":{"version":"1.0.0","createdAt":1777972964715,"changelog":"Initial release: systematic diagnostic tool for testability obstacles in legacy code.\n\n- Identifies why a class or method cannot be placed under test, routing to specific dependency-breaking techniques from \"Working Effectively with Legacy Code.\"\n- Guides users through construction tests, root cause classification, and obstacle identification using Feathers’ catalogs (class-level and method-level).\n- Provides task sequencing and rationale for each dependency-breaking technique.\n- Documents precise signals, classification steps, and recommended actions for both compile-time and runtime obstacles.\n- For use during dependency-breaking (Step 3) of the legacy code change process; not for test quality or new code design scenarios.","license":"MIT-0"},"metadata":{"setup":[],"os":null,"systems":null},"owner":{"handle":"quochungto","userId":"s176b6gfk8djgcz320d83ta4e184bx1v","displayName":"Hung Quoc To","image":"https://avatars.githubusercontent.com/u/88069966?v=4"},"moderation":null}