I have to say that in my opinion, the entire notion of Dependency Injection is overrated.
The example below shows how to achieve the exact same result for testing purposes as using DI, but with no additional baggage or requirements.
DI is the modern day equivalent of global values. The things you are injecting are global singletons and pure code objects, otherwise, you couldn't inject them. And the reason you Most uses of DI are injecting them is becauseforced on you don't have an architecture that allows accessin order to them any other way - all you have isuse a ton o' beans built on masses of boilerplate codegiven library (JPA, Spring Data, etc). For the most part, DI provides the perfect environment for nurturing and cultivating spaghetti.
The result is exactly equivalent to using DI - that is, the ClassUnderTest is configured for testing. The
The only differences are that this code is moreutterly concise, completely encapsulated, easier to code, easier to understand, faster, uses less memory, does not require an alternate configuration, does not require any frameworks, will never be the cause of a 4 page (WTF!) stack tracestrace that includeincludes exactly ZERO (0) classes which you wrote, and is completely obvious to everyoneanyone with even the slightest OO knowledge, from beginner to Guru (you would think, but would be mistaken). That
That being said, of course we can't use it - it's too obvious and not trendy enough.
InAt the meantimeend of the day, for us advanced guysthough, real injection means choosing and instantiatingmy biggest concern with DI is that of the desired class at run-time. That means we use interfacesprojects I have seen fail miserably, all of them have been massive code bases where DI was the way they were meant to be usedglue holding everything together. DI is not an architecture - to specifyit is really only relevant in a familyhandful of related classessituations, not as an alternatemost of which are forced on you in order to newuse another library (JPA, Spring Data, etc). For the most part, in a well designed code base, most uses of DI would occur at a level below where your daily development activities take place.