<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Shyam &#187; unit testing</title>
	<atom:link href="http://theshyam.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://theshyam.com</link>
	<description>Ramblings from the Real Shyam; You know, unlike those other fake Shyams!</description>
	<lastBuildDate>Sun, 08 Jan 2012 13:07:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Testing function vs testing implementation</title>
		<link>http://theshyam.com/2009/09/testing-function-vs-testing-implementation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-function-vs-testing-implementation</link>
		<comments>http://theshyam.com/2009/09/testing-function-vs-testing-implementation/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 17:18:19 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[implementation]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://theshyam.com/?p=215</guid>
		<description><![CDATA[Often I have got complaints from developers that I work with that their unit tests are prone to breakages, or they don&#8217;t like writing unit tests because their code changes frequently, which causes them to change their tests as well. Its just extra overhead at that point, and starts being a chore. Atleast thats what [...]]]></description>
			<content:encoded><![CDATA[<p>Often I have got complaints from developers that I work with that their unit tests are prone to breakages, or they don&#8217;t like writing unit tests because their code changes frequently, which causes them to change their tests as well. Its just extra overhead at that point, and starts being a chore. Atleast thats what their claim is. Now of course, I don&#8217;t agree with this at all. Not. One. Bit.</p>
<p>You see, when I hear this, its always tells me that there is something wrong with the way tests are written. A unit test that requires changes every time someone changes the code implies that there is a extremely strong coupling between how the code is written to how its tested. Some useful indicators of such a thing could be having a getter methods or properties which are visible only for tests, but not to external code. Or Tests which check if a loop happened 6 times or a mock was called 17 times. Sure, these assert that the function is working as intended, but say you optimize and reduce the recursion or method calls, then you need to go and update your expectations.</p>
<p>Of course, some of this is unavoidable when you are working with classes that have mocks injected into them. But in such a case, unless it is plain delegation, there must be some logic that must be happening. That should be the target of your tests, not the mock delegations. Usually, when I work with mocks, I have a few tests to make sure the right methods are getting called, and only if there is logic, I test it further. Otherwise, 1 or 2 tests and then I go and test the implementation of the mocked class to make sure it works under all conditions.</p>
<p>So lets consider a run of the mill binary search method that would be tested with mocks (A little bit contrived, but bear with me on this) :</p>
<pre>public int binarySearch(List&lt;Integer&gt; items, int itemToFind, int low, int high) {
<span style="background-color: #ffffff;">    // Do the needful, in a recursive fashion 
}</span></pre>
<pre>// A Brittle test
<span style="background-color: #ffffff; ">public void testUsingMocks() {
  List&lt;Integer&gt; list = mockery.mock(List.class);
  mockery.checking(new Expectations() {{
    oneOf(list).size(); will(returnValue(3));
    oneOf(list).get(1); will(returnValue(6));
  }});
  assertEquals(1, binarySearch(list, 6, 0, 2));
}</span></pre>
<p>Now, while a bit contrived, this is a familiar sight when mocks are used to test. Or it might happen that to check the correctness of the algorithm, the indices at which the split happens is stored in a list, and verified in the test. These are the kind of whitebox tests that make unit tests brittle. And the more of them there are, the harder it is to maintain or refactor code. Rather than testing it with for some use cases and boundary conditions, this is testing whether the algorithm itself is correct. Useful for some particular cases, but normally not required unless you are developing algorithm.</p>
<p>I would argue that its rare to write these kinds of tests if you write your tests before you write the methods. With a TDD, you just write your expectations, what you expect to give the method and what you expect out. You then write your code to get it to pass, and you might use internal variables or logic which the test really doesn&#8217;t care about. These tests are durable and hold up to refactorings, and even give you a nice safety net. There are times when these end up becoming integration tests rather than unit tests, but I still believe that they deliver more bang for the buck.</p>
<p>Of course, when you start testing edge cases, you do end up getting mostly a code dependent white box test, and those still are fine since they are supposed to be edge cases, which shouldn&#8217;t change that often. Though the fact that there are conditionals usually signifies that there is a polymorphic object hiding in there. But thats a blog post for another day.</p>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2009/09/testing-function-vs-testing-implementation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The ROI of Testing</title>
		<link>http://theshyam.com/2009/07/the-roi-of-testing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-roi-of-testing</link>
		<comments>http://theshyam.com/2009/07/the-roi-of-testing/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 21:15:29 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[reusability]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://assertionfailed.wordpress.com/?p=80</guid>
		<description><![CDATA[Nowadays, when I talk with (read: rant at) anyone about why they should do test driven development or write unit tests, my spiel has gotten extremely similar and redundant to the point that I don&#8217;t have to think about it anymore. But even when I do pairing with skeptics, even as I cajole and coax testable [...]]]></description>
			<content:encoded><![CDATA[<p>Nowadays, when I talk with (read: rant at) anyone about why they should do test driven development or write unit tests, my spiel has gotten extremely similar and redundant to the point that I don&#8217;t have to think about it anymore. But even when I do pairing with skeptics, even as I cajole and coax testable code or some specific refactorings out of them, I wonder, why is it that I have to convince you of the worth of testing ? Shouldn&#8217;t it be obvious ?</p>
<p>And sadly, it isn&#8217;t. Not to many people. To many people, I come advocating the rise of the devil itself. To others, it is this redundant, totally useless thing that is covered by the manual testers anyway. The general opinion seems to be, &#8220;I&#8217;m a software engineer. It is my job to write software. Nowhere in the job description does it say that I have to write these unit tests.&#8221; Well, to be fair, I haven&#8217;t heard that too many times, but they might as well be thinking it, given their investment in writing unit tests. And last time I checked, an engineer&#8217;s role is to deliver a working software. How do you even prove that your software works without having some unit tests to back you up ? Do you pull it up and go through it step by step, and start cursing when it breaks ? Because without unit tests, the odds are that it will.</p>
<p>But writing unit tests as you develop isn&#8217;t just to prove that your code works (though that is a great portion of it). There are so many more benefits to writing unit tests. Lets talk in depth about a few of these below.</p>
<h4 style="font-size: 1em;">Instantaneous Gratification</h4>
<p>The biggest and most obvious reason for writing unit tests (either as you go along, or before you even write code) is instantaneous gratification. When I write code (write, not spike. That is a whole different ball game that I won&#8217;t get into now), I love to know that it works and does what it should do. If you are writing a smaller component of a bigger app (especially one that isn&#8217;t complete yet), how are you even supposed to know if what you just painstakingly wrote even works or not ? Even the best engineers make mistakes.</p>
<p>Whereas with unit tests, I can write my code. Then just hit my shortcut keys to run my tests, and voila, within a second or two, I have the results, telling me that everything passed (in the ideal case) or what failed and at which line, so I know exactly what I need to work on. It just gives you a safety net to fall back on, so you don&#8217;t have to remember all the ways it is supposed to work in. Something tells you if it is or not.</p>
<p>Also, doing Test Driven Development when developing is one of the best ways to keep track of what you are working on. I have times when I am churning out code and tests, one after the other, before I need to take a break. The concept of TDD is that I write a failing test, and then I write just enough code to pass that test. So when I take a break, I make it a point to leave at a failing test, so that when I come back, I can jump right back into writing the code to get it to pass. I don&#8217;t have to spend 15 &#8211; 20 minutes reading through the code to figure out where I left off. My asserts usually tell me exactly what I need to do.</p>
<h4 style="font-size: 1em;">Imposing Modularity / Reusability</h4>
<p>The very first rule of reusable code is that you have to be able to instantiate an instance of the class before you can use it. And guess what ? With unit tests, you almost always have to instantiate an instance of the class under test. Therefore, writing a unit test is always a first great step in making code reusable. And the minute you start writing unit tests, most likely, you will start running into the common pain points of not having injectable dependencies (Unless of course, you are one of the converts, in which case, good for you!).</p>
<p>Which brings me to the next point. Once you start having to jump through fiery hoops to set up your class just right to test it, you will start to realize when a class is getting bloated, or when a certain component belongs in its own class. For instance, why test the House when what you really want to test is the Kitchen it contains. So if the Kitchen class was initially part of the House, when you start writing unit tests, it becomes obvious enough that it belongs separately. Before long, you have modular classes which are small and self contained and can be tested independently without effort. And it definitely helps keep the code base cleaner and more comprehensible.</p>
<h4 style="font-size: 1em;">Refactoring Safety Net</h4>
<p>Any project, no matter what you do, usually ends up at a juncture where the requirements change on you. And you are left with the option of refactoring your codebase to add / change it, or rewrite from scratch. One, <strong>never</strong> rewrite from scratch, always refactor. Its always faster when you refactor, no matter what you may think. Two, what do you do when you have to refactor and you don&#8217;t have unit tests ? How do you know you haven&#8217;t horribly broken something in that refactor ? Granted, IDE&#8217;s such as Eclipse and IntelliJ have made refactoring much more convenient, but adding new functionality or editing existing features is never simple.</p>
<p>More often than not, we end up changing some undocumented way the existing code behaved, and blow up 10 different things (it takes skill to blow up more, believe me, I have tried). And its often something as simple as changing the way a variable is set or unset. In those cases, having unittests (remember those things you were supposed to have written?) to confirm that your refactoring broke nothing is godsend. I can&#8217;t tell you the amount of times I have had to refactor a legacy code base without this safety net. The only way to ensure I did it correct was to write these large integration tests (because again, no unit tests usually tends to increase the coupling and reduce modularity, even in the most well designed code bases) which verified things at a higher level and pray fervently that I broke nothing. Then I would spend a few minutes bringing up the app everytime, and clicking on random things to make sure nothing blew up. A complete waste of my time when I could have known the same thing by just running my unit tests.</p>
<h4 style="font-size: 1em;">Documentation</h4>
<p>Finally, one of my favorite advantages to doing TDD or writing unit tests as I code. I have a short memory for code I have written. I could look back at the code I wrote two days ago, and have no clue what I was thinking. In those cases, all I have to do is go look at the test for a particular method, and that almost always will tell me what that method takes in as parameters, and what all it should be doing. A well constructed set of tests tell you about valid and invalid inputs, state that it should modify and output that it may return.</p>
<p>Now this is useful for people like me with short memory spans. But it is also useful, say, when you have a new person joining the team. We had this cushion the last time someone joined our team for a short period of time, and when we asked him to add a particular check to a method, we just pointed him to the tests for that method, which basically told him what the method does. He was able to understand the requirements, and go ahead and add the check with minimal handholding. And the tests give a safety net so he doesn&#8217;t break anything else while he was at it.</p>
<p>Also useful is the fact that later, when someone comes marching through your door, demanding you fix this bug, you can always make sure whether it was a a bug (in which case, you are obviously missing a test case) or if it was a feature that they have now changed the requirements on (in which case you already have a test which proves it was your intent to do it, and thus not a bug).</p>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2009/07/the-roi-of-testing/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Separation anxiety ?</title>
		<link>http://theshyam.com/2009/07/separation-anxiety/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=separation-anxiety</link>
		<comments>http://theshyam.com/2009/07/separation-anxiety/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 07:57:19 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[code smells]]></category>
		<category><![CDATA[constructor]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Law of Demeter]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://assertionfailed.wordpress.com/?p=70</guid>
		<description><![CDATA[We all have separation anxiety. Its a human tendency. We love inertia, and we don&#8217;t like change. But why should your code have separation anxiety ? Its not human (even though it might try and grow a brain of its own at times)! I bring this up because I got the chance to work with [...]]]></description>
			<content:encoded><![CDATA[<p>We all have separation anxiety. Its a human tendency. We love inertia, and we don&#8217;t like change. But why should your code have separation anxiety ? Its not human (even though it might try and grow a brain of its own at times)!</p>
<p>I bring this up because I got the chance to work with someone who had some questions on how to test UI code. Fairly innocuous question, and in most cases, my response would have been, keep the UI code simple and free of any logic, and don&#8217;t worry too much about it. Or you could write some nice end to end / integration tests / browser based tests. So with that response set in mind, I set off into the unknown. Little was I to know was that was the least of my concerns. As I sat down to look at the code, I saw that there were already tests for the code. I was optimistic now, I mean, how bad could things be if there are already tests for it ?</p>
<p>Well, I should remember next time to actually look at the tests first. But anyways, there were tests, so I was curious what kinds of tests they wanted to write and what kind of help I could provide, if any. So it turns out, the class was some sort of GUI Component, which basically had some fields, and depending on whether they were set of not, displayed them as widgets inside of it. So there were, say, 5 fields, and differing combinations of what was set would produce different output. The nice thing was that the rendered data was returned as a nice Java object, so you could easily assert on what was set, and get a handle on the widgets inside of it, etc. So the method was something along the following lines :</p>
<pre><span style="color:#333333;">public RenderedWidgetGroup render() {
</span><span style="color:#333333;">    </span><span style="color:#333333;">RenderedWidgetGroup group =
        createWidgetGroup(this.componentId,
                          this.parent);</span>
<span style="color:#333333;">    if (this.name = null) {</span><span style="color:#333333;">
</span><span style="color:#333333;">        return group;</span><span style="color:#333333;">
</span><span style="color:#333333;">    } </span><span style="color:#333333;">
</span><span style="color:#333333;">    group.addWidget(new TextWidget(this.name));</span><span style="color:#333333;">
</span><span style="color:#333333;">    group.addWidget(
        new DateWidget(
            this.updatedTimestamp == null ?
                 this.createdTimestamp : this.updatedTimestamp));
</span><span style="color:#333333;">    </span><span style="color:#333333;">return group;</span><span style="color:#333333;">
</span><span style="color:#333333;">}</span></pre>
<p>So far, so good, right ? Nice, clean, should be easy to test if we can set up this component with the right fields. After that, it should just be a few tests based on the different code paths defined by the conditionals. Yeah, thats what I thought too. So, me, being the naive guy that I was, said, yeah, that  looks nice, should be easy to test. I don&#8217;t see the problem.</p>
<p>Well, then I was taken to the tests. The first thing I see is a huge test. Or atleast thats what I think it is. Then I read it more closely, and see its a private method. It seems to take in a bunch of fields (Fields with names that I distinctly remembered from just a while ago) and churn out a POJO (Plain Old Java Object). Except this POJO was not the GUI Component object I expected. So obviously, I was curious (and my testing sensors were starting to tingle).  So I followed through to where it was called (which wasn&#8217;t very hard, it was a private method) and lo and behold, my worst fears confirmed.</p>
<p>The POJO object generated by the private method was passed in to the constructor of the GUI component, which (on following it further down the rabbit hole) in its constructor did something like the following :</p>
<pre><span style="color:#333333;">public MyGUIComponent(ComponentId id,
                      Component parent,
                      MyDataContainingPOJO pojo) {
    super(id, parent);
    readData(pojo); 
} </span></pre>
<p>And of course, <strong>readData</strong>, as you would expect, is a :</p>
<ul>
<li>Private method</li>
<li>Looks through the POJO</li>
<li>If it finds a field which is not null then it sets it in the Gui Component</li>
</ul>
<p>And of course, without writing the exact opposite of this method in the unit test, it just wasn&#8217;t possible to write unit tests. And sudddenly, it became clear why they were having trouble unit testing their Gui Components. Because if they wanted to test render (Which was really their aim), they would have to set up this POJO with the correct fields (which of course, to make things more interesting / miserable, had sub POJOs with sub POJOs of their own. Yay!) to be exercised in their test.</p>
<p>The problem with this approach is two fold :</p>
<ol>
<li>I need tests to ensure that the parsing and reading from the POJO logic is sound, and that it correctly sets up the GUI Component.</li>
<li>Every time I want to test the render logic, I end up testing (unintentionally, and definitely unwantedly) the parsing logic.</li>
</ol>
<p>This also adds, as I saw, obviously complicated pre test setup logic which should not be required to test something completely different. This is a HUGE <strong>code smell</strong>. Unit testing a class should not be an arduous, painful task. It should be easy as setting up a POJO and testing a method. The minute you have to perform complicated setup to Unit test a class (Note, the keyword is unit test. You can have integration tests which need some non trivial setup.), stop! There is something wrong.</p>
<p>The problem here is that there is a mixing of concerns in the MyGuiComponent class. As it turns out, MyGuiComponent breaks a few fundamental rules of testability. One, it does <strong>work in the constructor</strong>. Two, it violates the <strong>law of demeter</strong>, which states that the class should ask for what it needs, not what it doesn&#8217;t need to get what it needs (Does that even make sense ?). Thirdly, it is <strong>mixing concerns</strong>. That is, it does too much. It knows both how to create itself as well as do the rendering logic. Let me break this down further :</p>
<h3>Work in the constructor</h3>
<p>If you scroll back up and look at the constructor for MyGuiComponent, you will see it calling <strong><em><span style="color:#808080;">readData(pojo)</span></em></strong>. Now, the basic concept to test any class is that you have to create an instance of the class under test (unless it has static methods. Don&#8217;t get me started on that&#8230;). So now, every time you create an instance of MyGuiComponent, guess what ? <strong><em><span style="color:#808080;">readData(pojo)</span></em></strong> is going to get called. <strong>Every. Single. Time</strong> ! And it cannot be mocked out. Its a private method. And god forbid if you really didn&#8217;t care about the pojo and passed in a null. Guess what ? It most probably will blow up with a NullPointerException. So suddenly, that innocuous method in the constructor comes back to haunt you when you write yours tests (You are, aren&#8217;t you ?).</p>
<h3>Law of Demeter</h3>
<p>Furthermore, if you look at what <strong><em><span style="color:#808080;">readData(pojo)</span></em></strong> does, you would be even more concerned. At its base, MyGuiComponent only cares about 6 fields which it needs to render. Depending on the state of each of these fields, it adds widget. So why does the constructor ask for something totally unrelated ? This is a fundamental violation of the Law of Demeter. The Law of Demeter can be summed up as &#8220;Ask for what you need. If you need to go through one object to get what you need, you are breaking it.&#8221;. A much more fancier definition can be found on the web if you care, but the minute you see something like A.B().C() or something along those lines, there is a potential violation.</p>
<p>In this case, MyGuiComponent doesn&#8217;t really care about the POJO. It only cares about some fields in the POJO, which it then assigns to its own fields. But the constructor still asks for the POJO instead of directly asking for the fields. What this means is that instead of just creating an instance of MyGuiComponent with the required fields in my test, I now have to create the POJO with the required fields and pass that in instead of just setting it directly. Convoluted, anyone ?</p>
<h3>Mixing Concerns</h3>
<p>Finally, what could be considered an extension of the previous one, but deserves a rant of its own. What the fundamental problem with the above class is that it is mixing concerns. It knows both how to create itself as well as how to render itself once it has been created. And the way it has been coded enforces that the creation codepath is executed every single time. To provide an analogy for how ridiculous this is, it is like a a Car asking for an Engine Number and then using that number to create its own engine. Why the heck should a car have to know how to create its engine ? Or even a car itself ? Similarly, why should <strong>MyGuiComponent</strong> know how to create itself ? Which is exactly what is happening here.</p>
<h3>Solution</h3>
<p>Now that we had arrived at the root of the problem, we immediately went about trying to fix it. My immediate instinct was to pull out MyGuiComponent into the two classes that I was seeing. So we pulled out a MyGuiComponentFactory, which took up the responsibility of taking in the POJO and creating a GuiComponent out of it. Now this was independently testable. We also added a builder pattern to the MyGuiComponent, which the factory leveraged.</p>
<pre><span style="color:#333333;">class MyGuiComponentFactory {</span><span style="color:#333333;">
</span><span style="color:#888888;"><span style="color:#333333;">    MyGuiComponent createFromPojo(ComponentId id,
                                  Component parent,
                                  MyDataContainingPOJO pojo) {
      // Actual logic of converting from pojo to
      // MyGuiComponent using the builder pattern
     }
}</span></span>
<span style="color:#333333;">class MyGuiComponent {</span><span style="color:#333333;">
</span><span style="color:#333333;">    public MyGuiComponent(ComponentId id,
                          Component parent) {
        super(id, parent);
    }</span><span style="color:#333333;">
</span><span style="color:#333333;">    public MyGuiComponent setName(String name) {
        this.name = name;
        return this;
    }</span><span style="color:#333333;">
</span><span style="color:#333333;">} </span></pre>
<p>And now, suddenly (and expectedly, I would like to add), the constructor for MyGuiComponent becomse simple. There is no work in the constructor. The fields are set up through setters and the builder pattern. So now, we started writing the unit tests for the render methods. It took about a single line of setup to instantiate MyGuiComponent, and we could test the render method in isolation now. Hallelujah!!</p>
<p>Disclaimer :<br />
No code was harmed / abused in the course of the above blog post. There were no separation issues whatsoever in the end, it was a clean mutual break!</p>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2009/07/separation-anxiety/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My code&#8217;s untestable !!</title>
		<link>http://theshyam.com/2008/09/my-codes-untestable/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=my-codes-untestable</link>
		<comments>http://theshyam.com/2008/09/my-codes-untestable/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 21:53:33 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[code smells]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://assertionfailed.wordpress.com/?p=56</guid>
		<description><![CDATA[I have frequently heard this complaint, from some really great engineers in the past year. My code&#8217;s untestable, there&#8217;s no way i can test this, the only way to test my code is to write a full on end to end test. And in some cases, it was actually true. But the thing is, it [...]]]></description>
			<content:encoded><![CDATA[<p>I have frequently heard this complaint, from some really great engineers in the past year. My code&#8217;s untestable, there&#8217;s no way i can test this, the only way to test my code is to write a full on end to end test. And in some cases, it was actually true. But the thing is, it doesn&#8217;t need to be that way. There are always ways to twist and turn the untestable code into testable code, in other words refactor.</p>
<p>But before you get excited, and go aha, thats what I am going to do, hold your horses for a second. What are you going to go refactor ? Well, don&#8217;t scratch your heads, there are a few things to look out for. Similar to the code smells we had, there are similar smells we can look for, which indicate that the code is untestable. And there are a few standard ways to tackle them, and make your classes testable. So without further adieu, lets take a look at the more common and annoying testability smells.</p>
<ol>
<li><strong>Constructor doing work :</strong><br />
This is one of the biggest things preventing a class from being testable. There are many names to this smell, including constructor doing work,  Breaking the law of demeter, etc. but all it comes down to is the constructor doing more than just assigning stuff to local variables. For example, something like :</p>
<p>XPathConvertor() {<br />
this.xpathDatabase = XPathDatabaseFactory.getDatabase();<br />
XPathMapper mapper = new SimpleXPathMapper(&#8220;Simple Mapper&#8221;);<br />
this.xpathTranslator = XPathTranslatorFactory.getTranslator(this.xpathDatabase, mapper);<br />
}</p>
<p>While the above may seem a contrived example, or too simple, it exhibits what is at the centre of most bad constructors. One, its a default constructor. Then it goes out, grabs a database out from ether (Static factory method call), and then creates a mapper, and then passes those two to get a XpathTranslator object. Now, take my word for it, but the XPathConvertor only needs xpathTranslator. SO what is it doing with the darn database and the mapper. This is breaking the law of demeter, which states that &#8220;The constructor should only ask for what it needs, and nothing else.&#8221;</p>
<p>Why is this bad ? Well, for one, if the thing your constructor is creating is a heavy piece of service like a database or something, there&#8217;s a huge hit in your test. Your test is no longer a unit test, but an integration test. Each call now has to travel to the DB and back, and just makes everything slower. Secondly, there are some cases in which it picks up a service which you just can&#8217;t work with in a test. Something which either needs the whole production setup, or just doesn&#8217;t work in a unit test. And since it reaches into static factories to get that, there&#8217;s no way for you to slip in your mock.</p>
<p>So instead, start passing in what is needed to your constructor. This forms the basis of Dependency Injection, or slipping in a fake, or whatever you want to call it. Basically, your constructor takes in what it needs, and all it does is assign stuff to local variables. No work is done there. So the above code becomes something like :</p>
<p>XPathConvertor(XPathTranslator translator) {<br />
this.xpathTranslator = translator;<br />
}</p>
<p>So much cleaner, and only has what it needs. So in our test, you can create a translator and pass it in which uses a mock db, or pass in a fake translator or whatever. The point is, testing becomes easier.</li>
<li><strong>Global State :<br />
</strong> The second biggest complaint with untestable code. It usually has to do with Global state. Or as some people like to call it, putting things in and pulling things out of ether. This might be anything from using global static singletons to static method calls inbetween your method.</p>
<p>How is this bad ? Consider some method you are testing. What if it suddenly reaches out into the ether, and grabs some object and uses it to perform its calculations. You say, ok, I can somehow add a setter which allows me to set its state. Now what if there are multiple tests running in parallel&#8230;. Yes, exactly. Not good. Furthermore, you can&#8217;t mock a static method, which makes life miserable.</p>
<p>Consider this question then, why does it need to be static ? What benefit are you getting, other than the fact that you don&#8217;t need to create an object ? Is it really worth making code untestable in order to reduce one line of code of creating an object ? The answer 9 times out of 10, I find, is no.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2008/09/my-codes-untestable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fakes and Mocks and Stubs, Oh My!!!</title>
		<link>http://theshyam.com/2008/08/fakes-and-mocks-and-stubs-oh-my/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fakes-and-mocks-and-stubs-oh-my</link>
		<comments>http://theshyam.com/2008/08/fakes-and-mocks-and-stubs-oh-my/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 03:50:10 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[fake]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[stub]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://assertionfailed.wordpress.com/?p=42</guid>
		<description><![CDATA[So we covered how to use EasyMock to write tests in one of the previous posts, but Mocking is not the only option when you want to test something that depends on a slow and expensive service. Mocking allows you to expect calls and return values when a call is made, and make sure the [...]]]></description>
			<content:encoded><![CDATA[<p>So we covered how to use EasyMock to write tests in one of the previous posts, but Mocking is not the only option when you want to test something that depends on a slow and expensive service. Mocking allows you to expect calls and return values when a call is made, and make sure the number of calls were correct and the order, but sometimes, you want a bit more, or not even that much. So in those cases, you have the options of using Stubs or Fakes. So without further delay, your options are :</p>
<ol>
<li><strong>Mocks </strong>:<br />
Well, Mocks&#8230; What can I say. I have already ranted and raved about the awesomeness that is Mocks. Mocks and Mocking frameworks allow you to replace heavy and expensive services with Mocks, with which you can <strong>set expectations </strong>on what calls are made to the service, and return values. This gives you the advantage of knowing exactly what is called and returned, and makes your test deterministic and super fast. You can even set expectations on the number of calls and the order, though this is something that is not extremely recommended, since that makes the test dependent on the implementation, which is never a good thing.<em><strong> </strong></em></p>
<p><em><strong>Advantages</strong></em> :</p>
<ul>
<li>Fast, Reliable</li>
<li>Deterministic</li>
<li>Lightweight</li>
<li>Control over expectations and return values.</li>
</ul>
<p><em><strong> Disadavantages</strong></em> :</p>
<ul>
<li>Can become dependent on implementation of method being tested.</li>
<li>Can become a mockery if not careful, that is, can be testing interaction between mocks and nothing of the actual code. Especially when all that the method under test is doing is delegating to a bunch of service calls.</li>
<li>Can involve complicated setup for expectations, especially with unwieldy objects</li>
</ul>
</li>
<li><strong>Stubs</strong> :<br />
Now, whereas with Mocks, you can specify what calls will be received and what will be returned making them somewhat intelligent, Stubs are at the other end of the spectrum. The dumbest of the dumb, the easiest of the easiest, these just stub out the methods of your expensive and heavy class. So a <strong>null Stub </strong>could return null for each method, or just return some constant value each time it is called. That means that regardless of what the method is called with, it <strong>returns the exact same thing, </strong>day in and day out. It doesn&#8217;t adjust, it doesn&#8217;t react, just returns. Of course, this means that you might not be able to test all the cases you want to inspect / test. But its simple, its easy, and of course, its fast.<em><strong> </strong></em></p>
<p><em><strong>Advantages</strong></em> :</p>
<ul>
<li>Fast, reliable</li>
<li>Simple</li>
<li>Consistent</li>
</ul>
<p><em><strong> Disadvantages </strong></em>:</p>
<ul>
<li>Dumb</li>
<li>Can&#8217;t exercise different cases without different stubs</li>
<li>Returns only one value consistently</li>
</ul>
</li>
<li><strong>Fakes</strong> :<br />
And finally, we come to the smartest (figuratively) of the lot. The Fakes. For once, being a fake can be good. While Stubs return the same thing again and again, and mocks return what you tell it to, Fakes are smarter. The easiest way to explain a Fake is with an example. Say your code depends on a heavy service like a Database. A <strong>FakeDatabase </strong>would be an <strong>in-memory implementation </strong>of the DB, thus making it faster while at the same time, providing the same logic as the normal DB would. There are different types of Fakes, like a <strong>listening / recording Fake </strong>which records all the values passed to it.<em><strong> </strong></em></p>
<p><em><strong>Advantages</strong></em> :</p>
<ul>
<li>Can test while preserving behavior of dependencies</li>
<li>Faster than using actual services</li>
<li>Can test complicated logic</li>
<li>Most comprehensive testing approach</li>
</ul>
<p><em><strong> Disadvantages</strong></em> :</p>
<ul>
<li>Can be complicated to set up</li>
<li>Not as fast as mocks / stubs</li>
<li>Not as easy to define expectations</li>
<li>When / Where do you test the fake (especially the complicated ones) ?</li>
</ul>
</li>
</ol>
<p>Thats my 2 cents on the different approaches to testing with dependencies. Now that we know what we can do with those darn heavy dependencies, I&#8217;ll talk more on how to make sure you can use these different approaches to test your code soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2008/08/fakes-and-mocks-and-stubs-oh-my/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

