Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Over many years, how has the use of mocking frameworks shaped Java code design, for better and worse, and what guidance would you give a large organisation now?

Mocking frameworks pushed Java towards interfaces and dependency injection, because code designed that way was easy to test with mocks, and that was a real improvement. But they also made it possible to test badly designed code with ten dependencies, which hid design problems, and inline mocking of statics and finals removed the last pressure to design for testability. My guidance today is to test behaviour, use real objects where they are cheap, mock only at boundaries, and use Testcontainers for real infrastructure.

Why this question is asked

A question like this is about judgement built over years. There is no API to recall; instead, the interviewer wants to hear how we connect tools to the code they produce, and whether we can turn that into guidance a large organisation can follow. In this answer, we will walk through the history in three phases, look at what each phase did to design, for better and for worse, and then turn those lessons into concrete guidance with an example of how it changes a real test.

1The Starting Point: Testing Before Mocking Frameworks

In the early 2000s, much Java code created its own collaborators. A service would call new OrderDao() inside a method, or reach for a singleton through OrderDao.getInstance(). Testing such a service meant testing the database too, because there was no seam, no place where a test could swap in something else.

Teams that wanted isolated tests wrote stubs by hand: small classes implementing the same interface and returning fixed values. That worked, but it was laborious, so it mostly happened in teams that already cared a lot about design.

2Phase 1: Mocks Rewarded Interfaces and Injection

Libraries such as EasyMock, and later Mockito, made it trivial to create a fake for any interface. Around the same time, Spring made dependency injection mainstream: collaborators were passed in, through a constructor or a setter, instead of being created inside the class.

The two fed each other. If a class took its collaborators through the constructor, a test could pass mocks in, and the test was fast and focused. So developers who wanted good tests were nudged towards small interfaces, explicit dependencies and single responsibilities. This is the 'for better' part of the story, and it was real: the testability pressure produced code that was easier to change.

Java
// Code that mocking rewarded: dependencies are visible and replaceable
public class InvoiceService {
private final CustomerRepository customers;
private final TaxCalculator tax;
public InvoiceService(CustomerRepository customers, TaxCalculator tax) { ... }
}
var service = new InvoiceService(mock(CustomerRepository.class), new FlatTax("0.20"));

3Phase 2: Mocks Made Bad Design Bearable

The same convenience had a cost. Writing a test with two mocks is pleasant; writing one with eleven mocks is unpleasant, and that unpleasantness used to be a signal that the class did too much. Mockito removed most of the pain. With @Mock fields and @InjectMocks, a class with eleven collaborators could be tested almost as easily as one with two, so the signal disappeared.

Tests also started to mirror implementations. Because mocks record calls, it became common to verify every interaction, and the tests then described how the code worked instead of what it did. Those tests passed while real bugs slipped through, because mocks returned whatever we told them, including behaviour the real database or HTTP API would never show. And they failed on every refactor, which made people afraid to improve the design. So mocking helped design in phase 1 and then, used carelessly, started to protect bad design.

4Phase 3: Inline Mocking Removed the Last Pressure

For years, some things could not be mocked: static methods, final classes, constructors. Tools such as PowerMock and JMockit could do it with bytecode tricks, but they were awkward, so most teams still introduced a seam instead, for example an injected Clock instead of a static call to LocalDate.now().

When Mockito's inline mock maker made mockStatic() and final mocking ordinary, and the default in Mockito 5, that pressure went away too. It is genuinely useful for legacy code that we cannot change yet. But it also means a new class can hide its dependency on the clock, on UUID generation or on a static HTTP helper, and still be 'unit tested'. The cost appears later, for example when the JDK restricts the agents that this kind of mocking relies on.

How mocking shaped Java design
PhaseFor betterFor worse
Mocks of interfaces, DIsmall interfaces, explicit dependenciesinterfaces with one implementation everywhere
Annotation-driven mockingfast, focused unit testsclasses with many dependencies felt fine
Verify-everything habitseffects like emails are checkedtests mirror code, break on refactor
Inline static and final mockslegacy code becomes testableno pressure to add seams

5Guidance for a Large Organisation Now

Taking these lessons together, the guidance I would give fits in four sentences. Test behaviour: assert on results and observable effects, not on the steps. Use real objects where they are cheap: value objects, domain logic, mappers and in-memory fakes are faster to use for real than to mock. Mock only at boundaries: the payment provider, the email service, a third-party API, things that are slow, costly or outside our control. And use Testcontainers for infrastructure: a real PostgreSQL or Kafka in a container now starts in seconds, so we no longer need to mock our repositories to keep tests fast.

Here is how that changes a typical test. The old style mocked the repository, the mapper and the tax calculator and verified each call. The new style uses a real tax calculator, a real mapper, a repository backed by Testcontainers in an integration test or an in-memory fake in a unit test, and a mock only for the external payment gateway.

Java
// Before: five mocks, five verifies, breaks on every refactor
// After: real logic, one boundary mock
var gateway = mock(PaymentGateway.class);
var service = new CheckoutService(new InMemoryOrders(), new FlatTax("0.20"), gateway);
Receipt r = service.checkout(cartWithTotal("50.00"));
assertEquals(new BigDecimal("60.00"), r.total()); // behaviour
verify(gateway).charge(any(), eq(new BigDecimal("60.00"))); // the one external effect
Step throughWhere each kind of test double belongs
  1. Domain logic and value objectsreal objects, no doubles
  2. Application servicesreal collaborators or in-memory fakes
  3. Our repositories and messagingTestcontainers with real PostgreSQL or Kafka
  4. Third-party boundariesmocks or WireMock: payment, email, external APIs

4 layers, from the top down. Step through them one at a time.

Finally, I would treat a hard-to-write test as design feedback again. If a class needs eight mocks or a static mock, the standard should ask the author to consider splitting the class or adding a seam before reaching for more mocking power.

6Follow-up questions

Does this mean mocks are bad?

No. Mocks are the right tool at boundaries we do not control, and for checking effects such as a sent message. The problem is mocking everything, including our own cheap logic, because then the tests check our assumptions rather than the system.

Are interfaces with one implementation a bad legacy of mocking?

Often they are unnecessary. Mockito can mock concrete classes, so an interface should exist because there are, or will be, several implementations or a real boundary, not only to make mocking possible.

How would you measure whether the guidance works?

Track defects that escape to production per area, how many tests a typical refactor breaks, and suite time. If escaped defects fall and refactors break fewer tests while suite time stays acceptable, the guidance is working.

7Conclusion

In this answer, we followed mocking through three phases: it first rewarded interfaces and dependency injection, then made badly designed classes and implementation-mirroring tests bearable, and finally, with inline mocking of statics and finals, removed the remaining pressure to design for testability. The guidance that follows is to test behaviour, use real objects where they are cheap, mock only at boundaries, use Testcontainers for infrastructure, and treat painful tests as design feedback.

What to say in the interviewAbout 80 seconds aloud

Open with

I see three phases. First, mocking frameworks together with Spring made interface-based, dependency-injected design the easy way to get fast tests, and that genuinely improved Java code: dependencies became explicit and small.

Then, if they want more

Second, the tools got so convenient that the pain which used to signal bad design disappeared. A class with eleven dependencies could be tested with eleven @Mock fields, and teams started verifying every call, so tests mirrored the implementation, broke on every refactor and still missed bugs, because mocks behave however we tell them to.

Third, inline mocking of statics and finals in Mockito removed the last push to add seams such as an injected Clock, which is useful for legacy code but lets new code hide dependencies.

My guidance now is: test behaviour through results and observable effects; use real objects where they are cheap, like domain logic, mappers and in-memory fakes; mock only at boundaries we do not control, such as payment providers and third-party APIs; and use Testcontainers for our own infrastructure, because a real PostgreSQL or Kafka starts in seconds now.

And I would treat a test that needs eight mocks or a static mock as design feedback, not as a tooling problem.

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.
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 "Over many years, how has the use of mocking frameworks shaped Java code design, for...": https://scrivio-interview-library.netlify.app/java/mocking/3c1affd607/