Quick revision
The short answer
The symptoms point to tests coupled to implementation: heavy mocking and verification of internal calls mean that refactoring breaks hundreds of tests, while behaviour bugs pass because nothing checks the actual outcomes. I would shift towards testing behaviour through public APIs with real collaborators where they are cheap and fakes for slow boundaries, use mutation testing with PIT on key modules to find tests that detect nothing, delete or rewrite low-value tests, track and fix or quarantine flaky ones, and agree team conventions for names, test data builders and what to mock.
What to remember
In this answer, we saw that a suite which breaks on refactoring and misses real bugs is checking implementation rather than behaviour, usually through heavy mocking and verification of internal calls. We fix it by testing behaviour through public APIs with real cheap collaborators and fakes for slow boundaries, measuring weak tests with PIT, deleting or rewriting low-value tests, quarantining and fixing flaky ones, and agreeing conventions for names, builders and mocking.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The symptoms point to tests coupled to implementation: heavy mocking and verification of internal calls mean that refactoring breaks hundreds of tests, while behaviour bugs pass because nothing checks the actual outcomes. I would shift towards testing behaviour through public APIs with real collaborators where they are cheap and fakes for slow boundaries, use mutation testing with PIT on key modules to find tests that detect nothing, delete or rewrite low-value tests, track and fix or quarantine flaky ones, and agree team conventions for names, test data builders and what to mock.
Why this question is asked
A large test suite that fails in both directions, breaking on harmless changes and missing real bugs, is one of the most frustrating situations a team can be in. It costs time every day and gives no confidence. Interviewers ask this scenario because the cause is usually the same, and the fix requires both technical changes and team agreements. In this answer, we will diagnose why the suite behaves this way, look at an example of a coupled test, then go through the changes: testing behaviour, measuring with mutation testing, pruning, dealing with flakiness, and conventions.
1Diagnosing the Two Symptoms
The two symptoms look like opposites, but they have one cause. A test should fail when behaviour changes and stay green when only the structure changes. This suite does the reverse. Refactoring changes structure without changing behaviour, yet hundreds of tests fail. Bugs change behaviour, yet the tests pass.
That combination means the tests are checking structure, meaning which methods call which, rather than behaviour, meaning what comes out. The usual source is a style where every class is tested in isolation with every collaborator mocked, and the test verifies the calls made to those mocks. Such a test describes the current implementation line by line. Change the implementation and the test breaks; leave a wrong result and the test, which never looks at results, passes.
2What a Coupled Test Looks Like
Here is a typical example from such a suite.
The test stubs the calculator's answer and never checks the service's result, so a wrong calculation is invisible. And it verifies three internal calls, so any restructuring breaks it. Multiply this by 4,000 and we get exactly the suite described: expensive to maintain and blind to bugs.
3Testing Behaviour Through Public APIs
The main shift is to test a unit of behaviour through its public entry point, with its real collaborators where they are cheap. The discount policy and the calculator are plain Java objects with no input or output, so there is no reason to mock them; we use the real ones and assert the result. Only slow or external boundaries, such as the database, a message broker or a payment provider, get replaced, and preferably by fakes: simple working implementations like an in-memory repository.
| Change | Mock-heavy test | Behaviour test |
|---|---|---|
| Refactor internals, same behaviour | Fails | Passes |
| Bug changes the result | Passes | Fails |
| Rename a collaborator method | Fails | Passes |
| New collaborator added | Fails or needs new stubs | Passes |
This test survives refactoring because it only knows the public method and the result, and it catches the calculation bug because it checks the result. Where the database matters, such as queries and constraints, we add integration tests with Testcontainers rather than mocking the repository.
4Measuring Which Tests Detect Faults
We cannot rewrite 4,000 tests at once, so we need to know where the weak ones are. Mutation testing gives that measurement. PIT makes small changes to the code, such as flipping a condition or replacing a return value, and runs the covering tests. If no test fails, the mutant survived, which means the tests execute that code without checking it.
Running PIT on the three or four modules where production bugs came from usually gives a clear picture. It is common to see line coverage of 85 percent alongside a mutation score of 40 percent, which is exactly the gap behind escaped bugs. The surviving mutants point to specific lines, which gives the team a concrete list of what to fix first.
5Pruning, Flakiness and Conventions
With that data, we delete or rewrite tests that add cost without value. A test that only verifies internal calls with stubbed results, or that tests a getter, can usually be deleted once a behaviour test covers the same rule. Deleting tests feels wrong to many engineers, so it helps to agree the criteria together and to delete in reviewed batches. A suite of 2,500 good tests is better than 4,000 that nobody trusts.
Flaky tests need their own process. We record each test's pass and fail history in CI, quarantine a test that fails intermittently so it no longer blocks merges, and fix or remove it within an agreed time, for example two weeks. Otherwise people learn to rerun builds until green, and a real failure gets rerun away too.
Finally, conventions stop the problem from returning. The team agrees on test names that describe behaviour, test data builders so each test only sets the fields it cares about, and a simple rule for what to mock: external systems and slow boundaries yes, our own plain classes no, and verify() only when the call is itself the behaviour, such as sending an email. These go into the review guidelines, with good and bad examples from the team's own code.
6Follow-up questions
Is mocking bad, then?
No. Mocks are the right tool when the interaction is the behaviour, such as checking that a payment provider is called exactly once with an idempotency key, or for slow external systems. The problem is mocking every collaborator by default, including plain objects with no side effects, which turns tests into a copy of the implementation.
Will tests with real collaborators not be slower?
Not noticeably, as long as the collaborators are in-memory Java objects. They run in microseconds. The slow parts are databases, networks and containers, and those are the boundaries we still replace with fakes in unit tests or cover in a smaller number of integration tests.
How do you pick which tests to rewrite first?
Start with modules where production bugs came from and where refactoring breaks the most tests, then use PIT's surviving mutants and the CI history of test failures on harmless changes. That focuses effort where both symptoms hurt the most.
7Conclusion
In this answer, we saw that a suite which breaks on refactoring and misses real bugs is checking implementation rather than behaviour, usually through heavy mocking and verification of internal calls. We fix it by testing behaviour through public APIs with real cheap collaborators and fakes for slow boundaries, measuring weak tests with PIT, deleting or rewriting low-value tests, quarantining and fixing flaky ones, and agreeing conventions for names, builders and mocking.
Open with
Those two symptoms together tell me the tests are checking the implementation instead of the behaviour. A good test fails when behaviour changes and stays green when only structure changes; this suite does the opposite.
Then, if they want more
The usual cause is mocking every collaborator and verifying the calls between them. Such a test stubs the calculator's answer, verifies that find, rateFor and apply were called, and never checks the result. So a refactoring breaks it, and a calculation bug passes.
To fix it, I would move to testing behaviour through public entry points with real collaborators where they are cheap, like plain policy and calculator classes, and fakes such as an in-memory repository for slow boundaries, with Testcontainers integration tests for real database behaviour.
To know where to start, I would run PIT mutation testing on the modules where production bugs came from; seeing 85 percent coverage next to a 40 percent mutation score makes the problem visible and gives a list of lines. Then we delete or rewrite low-value tests in reviewed batches, because fewer trusted tests beat many untrusted ones.
Flaky tests get tracked, quarantined and fixed within a deadline. Finally, the team agrees conventions: behaviour names, test data builders, and mock only external systems, verifying calls only when the call itself is the behaviour.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your team has 4,000 unit tests, yet bugs keep reaching production and every refactoring...": https://scrivio-interview-library.netlify.app/java/test-strategy/bb33770214/