CS FundamentalsHardGATE CS · TCS NQT Prime · Product Company SDE

Why does depending on abstractions (interfaces) rather than concrete classes, as the Dependency Inversion Principle recommends, make unit testing easier?

  1. A. It makes the code compile faster in every case
  2. B. It guarantees 100% code coverage automatically
  3. C. A test can substitute a lightweight mock or fake implementing the same interface, isolating the class under test from the real, possibly slow or unavailable, concrete dependency
  4. D. It removes the need to write any tests at all

Correct answer: C. A test can substitute a lightweight mock or fake implementing the same interface, isolating the class under test from the real, possibly slow or unavailable, concrete dependency

Coding against an interface lets a test supply an alternative implementation (a mock/fake) satisfying that same interface, so the unit under test can be exercised without invoking the real dependency's actual behaviour.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic