<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aditya Sreekumar on Medium]]></title>
        <description><![CDATA[Stories by Aditya Sreekumar on Medium]]></description>
        <link>https://medium.com/@asreekumar?source=rss-920c737cf4b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*2ohTBgDufLzQp3CaIQB6hQ.jpeg</url>
            <title>Stories by Aditya Sreekumar on Medium</title>
            <link>https://medium.com/@asreekumar?source=rss-920c737cf4b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 10:17:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@asreekumar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dependency injection — How it helps testing?]]></title>
            <link>https://medium.com/@asreekumar/dependency-injection-how-it-helps-testing-18f0cb09f394?source=rss-920c737cf4b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/18f0cb09f394</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Aditya Sreekumar]]></dc:creator>
            <pubDate>Mon, 22 Apr 2019 23:27:36 GMT</pubDate>
            <atom:updated>2019-04-22T23:44:26.155Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<h3>Dependency injection — How it helps testing</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yZhtCFidrT5-pQHw7Bzvdw.jpeg" /></figure><h4>Introduction</h4><p><strong>Dependency Injection</strong> is a paradigm in which the object does not worry about initializing its dependencies, but instead expects to receive it from somewhere else — thus facilitating modular code. A more theoretical explanation of this concept can be found in <a href="https://medium.freecodecamp.org/a-quick-intro-to-dependency-injection-what-it-is-and-when-to-use-it-7578c84fa88f?gi=44ec9bb768e">another article</a>.</p><p>The aim in this article is to walk through an example of where <strong>Dependency Injection</strong> is useful and see how it facilitates easier unit testing.</p><h4>Scenario</h4><p>For the purpose of this article, we consider the example of a home security system which needs to have the ability to ring a doorbell — ringDoorbell() — as well as keep track of how many times the doorbell has been rung — getNumberRings() .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bf5568a0182a3251866d83fe99a18ff/href">https://medium.com/media/6bf5568a0182a3251866d83fe99a18ff/href</a></iframe><h4>Basic OO approach</h4><p>If we start off with a trivial object oriented approach to this problem, we will create an object to encapsulate a door bell and use that in the SecuritySystem class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05b12a00ba09d3a85543ca6ff7167668/href">https://medium.com/media/05b12a00ba09d3a85543ca6ff7167668/href</a></iframe><p>Here, the SecuritySystem class creates an object of ButtonDoorBell. internally and uses that for the APIs related to ringing a door bell.</p><p>The design issue here is that if we want to replace the button based doorbell with a video doorbell, we would have to now modify the class definition. As you can probably imagine, the overhead with doing this every time we support a new type of doorbell would be cumbersome.</p><h4>Dependency Injection</h4><p>A better approach to the previous design problem is abstracting out the doorbell as an interface, and <strong>Dependency Injecting</strong> the concrete doorbell class.</p><p>To do this, we introduce a DoorBellIntf which specifies the APIs of a generic door bell. The ButtonDoorBell class will implement that interface to give us a definition of the required APIs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0c29df4b82010a34a598380c788ad5e/href">https://medium.com/media/d0c29df4b82010a34a598380c788ad5e/href</a></iframe><p>The SecuritySystem will use that interface and take in a concrete object in its constructor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ea7bf92100b58884dbde8088f1e0517/href">https://medium.com/media/0ea7bf92100b58884dbde8088f1e0517/href</a></iframe><h4>Extending the program</h4><p>If we wish to add a new type of doorbell now, we just need to dependency inject it at run-time with no changes necessary in SecuritySystem .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee09a47d479e7ca096d7d1009eca3d00/href">https://medium.com/media/ee09a47d479e7ca096d7d1009eca3d00/href</a></iframe><h4>Better Unit Testing</h4><p>The above code highlights the advantage of dependency injection, but why do we assert that doing it makes testing SecuritySystem easier?</p><p>The answer is quite simple — we can easily test the security system class for the ringBell API by creating a test version of the DoorBellIntf class. We also add some helpers which allow us to manipulate the return value of ring API.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fc10a180795b15a34e3ad12d4967fd5/href">https://medium.com/media/2fc10a180795b15a34e3ad12d4967fd5/href</a></iframe><p>We now use this TestDoorBell class to write a test which creates a test doorbell, rings the doorbell and check the return value and ensures it is false.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ef74bd736656f3a29d0d4d7252c5c1f/href">https://medium.com/media/9ef74bd736656f3a29d0d4d7252c5c1f/href</a></iframe><p>Now, we want to test what happens when the DoorBell returns true. This is simple as we already made a custom test helper to assist us:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7badec54c404b4bf13dd9463085f72a4/href">https://medium.com/media/7badec54c404b4bf13dd9463085f72a4/href</a></iframe><p>This ability to manipulate the dependent objects and test only the APIs we are concerned with is very important for unit testing and has been made much easier thanks to dependency injection. It would not have been possible had we proceeded with our initial approach.</p><p>For those familiar with other testing frameworks (GoogleTest, Catch2, etc), dependency injection can be used to inject Mocks, Fakes and other test doubles.</p><h4>Word of Caution</h4><p>Dependency injection is not without its downsides. If overdone, you can create classes which have every single API satisfied by a dependency injected object. Managing this becomes difficult and unwieldy. It also increases the complexity of code as it introduces a further level of indirection which might not be necessary.</p><p>If used properly however, dependency injection is a great way to create modular and testable code.</p><h4>Resources</h4><ol><li>GitHub code with examples which compile based on this article — <a href="https://github.com/Aditya90/DependencyInjectionExample">https://github.com/Aditya90/DependencyInjectionExample</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18f0cb09f394" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>