All jobs / Backend developer / Testing
Backend developer interview question · 3–5 years
A refactor that changed no behaviour broke forty tests. Each one asserts that a particular method was called on a mock.
What is the lesson?
The strongest answer
The tests were pinned to the implementation rather than the behaviour — mock only what crosses a boundary you do not own, and assert on outcomes, so a refactor that keeps the contract keeps them green
Why it works
Exactly — a test that goes red on a safe refactor is a maintenance bill, not a safety net.
What a good answer covers
- Assert behaviour, not calls.
- Mock at boundaries you do not own.
- A safe refactor should stay green.
- Brittle tests are a maintenance bill.
Answers that fall short
- Close, but not the strongest: There is too much mocking; the tests should use real objects wherever they can
Right direction, and the rule that makes it actionable is the boundary: mock the payment gateway, not your own collaborator. - Weak: That is the price of a large refactor, and forty tests changing shows the tests were thorough
Thorough tests catch behaviour changes. Forty of them turning red on a no-op refactor is the opposite signal. - Red flag: I would delete the failing tests: behaviour did not change, so they were not telling us anything
Deleting them without first learning what they covered throws away the one thing that would have caught a real break.
Who gets asked this
More Testing questions
- What would consumer-driven contract tests give you here?
- The service’s CI suite fails about one run in five for reasons unrelated to the change, and the team’s habit…
- A teammate calls their test a unit test, but it starts a database container and an HTTP server. Does the…
- In a test, what is the difference between a stub, a mock and a fake?
- Your repository tests run against an in-memory database and pass, but the same query fails in production…
- How do you test that POST /orders returns 201 with the right body, without deploying anything?