Important announcement

Dear visitor,

Please note that this OpenCourseWare platform will be replaced at the end of 2026 by a new platform (under the same url ocw.tudelft.nl).

Courses and their contents that are currently available the platform will partly be migrated. URL’s for this content will change and content might be unavailable for some time. In addition, some courses will not be available anymore.

If you are linking to specific courses on this platform (for instance for your students), please take timely measures to avoid problems with access. Possible actions are:

  • Download the materials to your local platform (laptop, server)
  • For educators only (including TU Delft lecturers): Contact us at ocw@tudelft.nl and inform us what course you are using in what context (institute, program, period of use, number of students). We will research (in collaboration with the authors of the course) and decide if we will migrate the course, and if yes, can supply you with a new url.

Thank you.

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
[sg_popup id=6]