What is dependency injection in testing?

Updated May 6, 2026

Short answer

DI allows passing dependencies instead of hardcoding them.

Deep explanation

It improves testability by allowing mocks or stubs to be injected.

Real-world example

Injecting mock API in service tests.

Common mistakes

  • Tight coupling without DI.

Follow-up questions

  • Why DI helps testing?
  • Is DI always required?

More Unit Testing interview questions

View all →