<?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 Nisar Ahmad on Medium]]></title>
        <description><![CDATA[Stories by Nisar Ahmad on Medium]]></description>
        <link>https://medium.com/@NisarGujjar11?source=rss-3b7de8f398c7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*oZnO36-Pz1VYn6_k3vKz1g.jpeg</url>
            <title>Stories by Nisar Ahmad on Medium</title>
            <link>https://medium.com/@NisarGujjar11?source=rss-3b7de8f398c7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 21:40:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@NisarGujjar11/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[Introduction to Unit Testing — Why should you start writing unit tests]]></title>
            <link>https://medium.com/@NisarGujjar11/introduction-to-unit-testing-why-should-you-start-writing-unit-tests-ee04c2de4?source=rss-3b7de8f398c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee04c2de4</guid>
            <category><![CDATA[unit-test-framework]]></category>
            <category><![CDATA[unit-test-tools]]></category>
            <category><![CDATA[unit-testing]]></category>
            <dc:creator><![CDATA[Nisar Ahmad]]></dc:creator>
            <pubDate>Tue, 01 Sep 2020 04:41:04 GMT</pubDate>
            <atom:updated>2020-09-01T04:41:04.654Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction to Unit Testing — Why should you start writing unit tests</h3><h3><strong>Unit testing is one of the hottest topics in agile methodologies along with the other things such as Integration testing and CI/CD</strong></h3><p><em>According to Wikipedia</em>, Unit Testing is a <a href="https://en.wikipedia.org/wiki/Software_testing">software testing</a> method by which individual units of <a href="https://en.wikipedia.org/wiki/Source_code">source code</a>, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use.</p><p>In simple words, The Unit test is the piece of code which is written to test the business logic/behavior of the other code</p><p>So few questions arise in the mind like<br>When code is testable?<br>What is the benefit of the unit tests?<br>What is the impact on the quality of the code?</p><p>Let&#39;s discuss these questions:</p><h4>When code is testable?</h4><p>Not all of the code can be unit tested, only those functions/piece of code can be unit tested if and only if their dependencies are being injected from outside, or the function/piece of code have the deterministic behavior.<br>Example:</p><blockquote>public String doSomething(Date someDate){<br> Date date = new Date();</blockquote><blockquote>if(date.isAfter(someDate)){<br> return “tomorrow”;<br> }else if(date.isBefore(someDate)){<br> return “Yesterday”;<br> }else{<br> return “Equal”;<br> }</blockquote><blockquote>}</blockquote><p>This example is as simple as it can get<br>If you in the above function date instance provides the today on each time and we compare today date with someDate.<br> This behavior is non-deterministic. Each instance of the running of this code will give a new date object for today. Hence it is not possible to write unit tests for this code.<br>Let&#39;s see what is the issue in unit testing with this code</p><pre>@Test<br>public void comparing7thwith10th_returnsyesterday() throws Exception {<br>    try{<br>//today date is 21 Aug 2020</pre><pre>        Date tomorrowDate = SimpleDateFormat(&quot;MM/dd/yyyy&quot;).parse(&quot;08/22/2020&quot;)<br>        asserEquals(doSomething(tomorrowDate),&quot;tomorrow&quot;);<br>    }catch(Exception e){<br><br>    }<br>}</pre><p>The result of the unit test will be passed only on 21st August and it will be failing before and after 21 August.</p><p>So the result is the above function has the non-deterministic behavior we can not write the unit test of this function. To write the unit test, first of all, we have to make it deterministic, How can we make it deterministic let see in below snippet of code</p><pre>public String doSomething(Date source, Date someDate){<br>    if(source.isAfter(someDate)){<br>        return &quot;Tomorrow&quot;;<br>    }else if(source.isBefore(someDate)){<br>        return &quot;Yesterday&quot;;<br>    }else{<br>        return &quot;Equal&quot;;<br>    }<br>}</pre><p>As in this function we two arguments source and someDate so both arguments are injected and the function behaves in a deterministic way. let’s write the unit test for this function</p><pre>@Test<br>public void comparingSameDates_returnsEqual() throws Exception {<br>    try{<br>        Date date = new date();<br>        asserEquals(doSomething(new Date(),new Date()),&quot;Equal&quot;);<br>    }catch(Exception e){<br><br>    }<br>}</pre><p>It is not a good practice to instantiate every dependency in the class itself. Dependencies should be provided externally to the class hence making the class more testable. That is why the concept of Dependency Injection is in trend these days.</p><h4>Impact on Code Quality</h4><p>It is a myth that unit tests are much time in development which may cause the delay in your plan. In TDD methodologies you have to write the unit test first then you start your implementation.<br>So what is the Impact of the unit tests on your code quality?<br>1 -&gt; Writing unit tests makes you think hard about the things such as architecture and design pattern of your code. As a result, you end up writing quality code that is elegant, easier to read, and most importantly easier to refactor in the future.<br>2 -&gt; Unit tests are acts as the documentation of the code and provide the higher-level picture of the implementation <br>3 -&gt; Unit Testing is carried out before integrating it with the main code. So, if your code has some unexpected behavior, it can be spotted before mixing it with the old code. This prevents a lot of headaches later on when you have integrated your code with the main working code.</p><h3>What’s next?</h3><p>In the next article, we’ll learn how to write the unit test in android using android studio.</p><p>Thanks, Any suggestion will be appricaited.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee04c2de4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>