Mocking & service virtualization

WireMock/MSW/Playwright route interception to isolate the system under test, simulate 500s, timeouts and slow dependencies you cannot trigger on demand.

About 23 min read3-question knowledge check

The mental model

A flight simulator does not need a real thunderstorm to train a pilot for one. It needs a faithful simulation of a thunderstorm, available on demand, repeatable a hundred times, and safe to crash in. Mocking and service virtualization are the flight simulator for your system's dependencies: the payment gateway, the SMS provider, the recommendation service, the third-party KYC API — replaced by controllable doubles that answer exactly the way you script them, including the ways the real thing answers only at 3am during an outage.

Without this capability, three things break. First, your test suite's reliability is capped by the least reliable dependency it touches: if the sandbox payment gateway is up 97% of the time, your suite can never exceed 97% green regardless of how good your tests are. Second, entire classes of behaviour become untestable: you cannot ask a real dependency to return a 500, hang for 30 seconds, or send back malformed JSON on demand — which means your error-handling code ships untested. Third, speed dies: a suite that makes real network calls to third parties runs 10–50x slower than one running against local doubles, and slow suites get run less, which means bugs are found later.

The vocabulary matters in interviews. A stub returns canned answers ("GET /user/42 always returns this JSON"). A mock additionally *verifies interaction* ("assert the client called POST /charge exactly once with this body"). A fake is a working lightweight implementation (an in-memory repository instead of Postgres). Service virtualization is the enterprise-scale version: a long-running simulated service, often with stateful behaviour and recorded traffic, shared across teams. The engineering judgment is knowing which boundary to double and which to leave real — double everything and you are testing your mocks; double nothing and you are testing the internet.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision