What is mocking in unit testing?

Updated May 6, 2026

Short answer

Mocking simulates dependencies to isolate the unit being tested.

Deep explanation

Mocking replaces real implementations like APIs or databases with fake versions. This ensures tests are fast, reliable, and independent of external systems.

Real-world example

Mocking an API call in a weather app test.

Common mistakes

  • Over-mocking and losing test realism.

Follow-up questions

  • What is the difference between mock and stub?
  • When should you avoid mocking?

More Unit Testing interview questions

View all →