4.4.4 Dependency injection vs Dependency Inversion Principle

Course subject(s) Module 4: Testability and Mock Objects

Two terms that sound the same, but mean different things are: dependency inversion and dependency injection.

First, Dependency inversion. Robert Martin defines the Dependency Inversion Principle as:

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend upon details. Details should depend upon abstractions.

So, dependency inversion is about how classes are designed. The principle makes us think about what parts of our system are (should be) abstractions and what parts  contain low level details.

Dependency injection, on the other hand, is about how one object knows the dependencies of other objects, so that it can inject them, when needed. Dependency Injection is about how objects get their dependencies.

As you see, both are different, but somewhat related.

A question for you: how do they both help us achieve testability?

Further reading:

  • Martin, Robert. OO Design Quality Metrics: An Analysis of Dependencies. URL: http://condor.depaul.edu/dmumaugh/OOT/Design-Principles/oodmetrc.pdf, 1994.
  • Schuchert, Brett L. DIP in the Wild. URL: https://martinfowler.com/articles/dipInTheWild.html, 2013.
  • Martin, R. C. (2002). Agile software development: principles, patterns, and practices. Prentice Hall.
Creative Commons License
Automated Software Testing: Practical Skills for Java Developers by TU Delft OpenCourseWare is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Based on a work at https://online-learning.tudelft.nl/courses/automated-software-testing-practical-skills-for-java-developers/.
Back to top