How would you design a focused test strategy for error handling while refactoring without changing behavior?
Answer
Build the smallest useful model of the behavior by making inputs, outputs, ownership and failure behavior explicit before choosing an implementation. Focus on error handling while refactoring without changing behavior. Use small deterministic units plus integration behavior at real boundaries as the oracle. Cover normal flow, invalid inputs, exceptions, concurrency and resource cleanup. Keep data and dependencies controlled enough to reproduce failures. Release only when tests fail for one understandable reason and the code remains easy to change.
Strong answer includes
- explicit risk and test oracle for error handling
- controls side effects and dependencies
- designs for deterministic tests
- measurable evidence and residual-risk statement
Practical example
Refactoring a sprawling try/catch block into smaller functions risked silently changing which exceptions got caught where. The strategy was to first write characterization tests asserting the exact exception type and message surfaced for each known failure input under the old code, then run the same tests unchanged against the refactored version before merging.