Software Testing & QA Hard technical 0 views 1 min read

How do you add tests to legacy code that has none?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of Software Testing & QA conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

Legacy code is often defined as code without tests, which makes changing it risky. The core technique is to introduce a seam, a place where behaviour can be substituted without editing the code under test.

Michael Feathers' approach:

  1. Identify a change point and find the smallest behaviour to pin down.
  2. Break dependencies by extracting interfaces, wrapping static or global calls, and injecting collaborators through constructors or parameters.
  3. Write characterisation tests that capture current behaviour, even if it looks wrong, so you know when you change it.
  4. Refactor under that safety net in small steps, then add tests for the new behaviour.
class Report {
    Report(Clock clock, Mailer mailer) { ... } // seams injected
}

Tools help: approval or golden-master tests capture output for comparison, and dependency-breaking tools can substitute calls at link time. The risk is over-mocking, so prefer real fakes where feasible. Do not aim for full coverage first; cover the areas you are about to change.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?