In a codebase with all dependencies injected, can unit testing catch regressions?

by James Ellis-Jones   Last Updated June 27, 2018 11:05 AM

Let's say I build my code so that I inject ALL dependencies to every class and when I test, I replace those dependencies with test doubles. Let's also say that when I work on a class I run its tests to test it. Given that a regression is an unexpected failure in a part of the codebase other than that which I changed, it must be due to code (say in class X) which has a dependency on some code I changed being broken by a change in the behaviour of my code. However, class X's dependencies have been replaced with test doubles when it is being tested and those have not changed. Therefore the unit tests on X will not fail. Therefore my unit tests will not catch any regressions caused by my changes.

Is this correct?

(I'm not interested in an explanation of why it is necessary for unit tests to not test dependencies, I'm assuming that's the case and questioning the consequences)

Answers 2

That is correct. And it is the fundamental flaw with the reductio ad absurdum view that unit tests must only test a single "unit" of code and that everything else should be mocked.

If you adopt the far more sensible approach of view a "unit" as a piece of functionality that can be run in isolation (ie, has no side effects, so can be tested in parallel), then this whole problem goes away. You only replace dependencies with test doubles when needed. If that dependency has a side-effect, or is slow, then push testing that interaction out into your integration tests. Otherwise leave it in place so that your unit tests form useful regression tests.

If you do follow that extreme position on mocking everything, then you'll end having to write lots of integration tests to provide that regression testing. Which then rather begs the question, what are those unit tests for, as they aren't then to provide regression testing?

David Arno
David Arno
June 27, 2018 10:27 AM

If you define the term "regressions" the way you did, and use the term "unit tests" really for testing isolated units (and not in the sloppy way several people do when they actually talk about automated tests), then you are correct - unit tests will not find regressions.

That is why you also also need regression tests, ideally automated ones. And when sticking to your wording, these are integration tests exclusively.

Doc Brown
Doc Brown
June 27, 2018 10:56 AM

Related Questions

Using Spring in Java Project

Updated February 29, 2016 01:02 AM

Dependency Injection with default Construction

Updated March 18, 2017 18:05 PM