<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>Testing @ clintshank.javadevelopersjournal.com</title><link>http://clintshank.javadevelopersjournal.com/</link><description>(Testing) My thoughts on software development.</description><copyright>Copyright 2008 clintshank.javadevelopersjournal.com</copyright><generator></generator><lastBuildDate>Fri, 05 Sep 2008 17:00:00 GMT</lastBuildDate><image><title>Testing @ clintshank.javadevelopersjournal.com</title><url>http://res.sys-con.com/portlet/163/featured-blog-graphic-145.gif</url><link>http://clintshank.javadevelopersjournal.com/</link></image><ttl>360</ttl><docs>http://backend.userland.com/rss</docs><item><title>Essence Over Ceremony in Unit Testing</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/essence_over_ceremony_in_unit_testing.htm</guid><link>http://clintshank.javadevelopersjournal.com/essence_over_ceremony_in_unit_testing.htm</link><pubDate>Sat, 09 Aug 2008 14:45:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=essence%5Fover%5Fceremony%5Fin%5Funit%5Ftesting</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>There&#39;s been some talk recently about essence and ceremony, particularly regarding JVM programming languages. My first remembrance of this discussion was reading <a href="http://blog.thinkrelevance.com/2008/4/1/ending-legacy-code-in-our-lifetime">this blog entry</a> from Stu Halloway.&nbsp; Java is a ceremonious language because there&#39;s a lot of extra, requried typing that blurs the essence of what you&#39;re trying to communicate in the code.</p><p>I want to talk about essence vs. ceremony in unit testing.</p><p>If I give you this:</p><pre>OrderTest<br />    testCalculatePrice<br />        1 20.00<br />        2  5.00<br />          30.00<br /></pre><p>Can you tell what this is about? This is a specification of the calculatePrice() method on an Order object, where there are two LineItem objects and the expected price is $30.00. Did you figure that out without the explanation? This is essence.</p><p>Instead, what you often see is something like this:</p><pre>public class OrderTest { </pre><pre>    @Test<br />    public void testCalculatePrice() {<br />        Order order = new Order();<br />        Product product1 = new Product(20.00);<br />        LineItem lineItem1 = new LineItem(1, product1);<br />        order.add(lineItem1);<br />        Product product2 = new Product(5.00);<br />        LineItem lineItem2 = new LineItem(2, product2);<br />        order.add(lineItem2);<br />        assertEquals(30.00, order.calculatePrice());<br />    }<br />}</pre><p>Here, you&#39;re blinded with irrelevant details that <a href="http://xunitpatterns.com/Obscure%20Test.html">obscure</a> what this test is all about. The essence of the specification is buried in details. What you want is clarity at this level.</p><p>You can factor out the obscurity with something like this:</p><pre>public class OrderTest {<br />    private Order order = new Order();</pre><pre>    @Test<br />    public void testCalculatePrice() {<br />        lineItem(1, 20.00);<br />        lineItem(2, 5.00);<br />        expectPrice(30.00);<br />    }<br />}</pre><p>That&#39;s gets us about as close as we can in Java.</p><p>A consequence of this approach is that you have more methods overall, but I&#39;ve always felt that clarity trumps in specifications. <a href="http://thediscoblog.com/2008/07/02/its-ok-to-wet-yourself-every-once-in-awhile/">Others have agreed</a>.</p><p>What&#39;s interesting with these helper methods is that I can change how I implement them and not change the higher level, essence methods at all. For example, in lineItem() I can inject real production objects (as I did in the original example with LineItem and Product), or I can inject a mock or stub LineItem.</p><p>So my suggestion is when you are specifying these high level methods, attempt to only show the essence of the test and factor out the ceremony.</p><p>&nbsp;</p>]]></description><category>testing</category><category>java</category></item><item><title>Law of Demeter and Unit Test Setup</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/long_unit_test_setup.htm</guid><link>http://clintshank.javadevelopersjournal.com/long_unit_test_setup.htm</link><pubDate>Sat, 01 Dec 2007 01:23:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=long%5Funit%5Ftest%5Fsetup</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>Have you ever seen or even developed a long, complex setup method in JUnit?&nbsp; Maybe it was because the production code was violating the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a>.&nbsp; Here&#39;s another <a href="http://www.objectmentor.com/resources/publishedArticles.html">Robert Martin Craftsman style</a> blog.</p><p><strong>Apprentice</strong>: &nbsp;Journeyman, I&#39;m having trouble writing the fixtures for my JUnit tests.&nbsp; They take too long.&nbsp; I spend about 10 times longer writing tests than the production code.</p><p><strong>Journeyman</strong>: &nbsp;Really?&nbsp; Unit test expert Gerard Meszaros <a href="http://www.se-radio.net/podcast/2007-10/episode-70-gerard-meszaros-xunit-test-patterns">says</a> that tests should take only 10% to 20% of development time.</p><p><strong>Apprentice</strong>: &nbsp;Well, I&#39;m experiencing the exact opposite.</p><p><strong>Journeyman</strong>: &nbsp;Let me take a look.</p><p><strong>Apprentice</strong>:&nbsp;&nbsp;Sure.</p><p><strong>Author Note</strong>: &nbsp;This is a trivial example for this blog entry.&nbsp; I&#39;ve seen some really obscure test setup methods.&nbsp;&nbsp;The example is based on the sequence diagram chapter in Martin Fowler&#39;s <a href="http://www.amazon.com/exec/obidos/ASIN/0321193687">UML Distilled</a> book.</p><pre>public class OrderTest {<br /> <br />     private Order order;<br /> <br />     @Before<br />     public void setUp() throws Exception {<br />         Product product1 = new Product(11.00);<br />         OrderLine line1 = new OrderLine(2, product1);<br />         <br />         Product product2 = new Product(22.00);<br />         OrderLine line2 = new OrderLine(3, product2);<br /> <br />         OrderLine[] lines = new OrderLine[] {line1, line2};<br />         order = new Order(Arrays.asList(lines));<br />     }<br />     <br />     <br />     @Test<br />     public void priceShouldBe88() {<br />         assertEquals(88.00, order.getPrice());<br />     }<br /> }<br /> &nbsp;</pre><p><strong>Journeyman</strong>:&nbsp; Okay, can I see the Order.getPrice() method?</p><pre>    public double getPrice() {<br />         double price = 0.00;<br />         <br />         for (OrderLine orderLine : orderLines) {<br />             Product product = orderLine.getProduct();<br />             double productPrice = product.getPrice();<br />             int quantity = orderLine.getQuantity();<br />             price += productPrice * quantity;<br />         }<br />         <br />         return price;<br />     }<br /> </pre><p><strong>Journeyman</strong>:&nbsp; Ah, I see the issue.&nbsp; Have you ever heard of the Law of Demeter (LoD)?</p><p><strong>Apprentice</strong>: &nbsp;Huh?</p><p><strong>Journeyman</strong>:&nbsp; The LoD has a few different names and related principles, but basically it means that an object should only deal with and only with its collaborators.</p><p><strong>Apprentice</strong>:&nbsp; Huh?</p><p><strong>Journeyman</strong>: You see how Order iterates through its OrderLine objects?</p><p><strong>Apprentice</strong>:&nbsp; Yeah.</p><p><strong>Journeyman</strong>:&nbsp; Well, that&#39;s fine.&nbsp; But then you grab the Product and then grab its price.&nbsp; You&#39;ve violated the LoD.&nbsp; Order should only know about OrderLines.&nbsp; You&#39;ve got more of a procedural design here, where you reach down into all the objects in the graph, grabbing all the data you need, and then do your thing.&nbsp; A more object oriented design would be to distribute the work.&nbsp; Do a little bit of work and delegate the rest to collaborating objects.</p><p><strong>Apprentice</strong>:&nbsp; Oh, I think I&#39;m beginning to see.</p><p><strong>Journeyman</strong>:&nbsp; You should go read up on Craig Larman&#39;s <a href="http://www.amazon.com/Applying-UML-Patterns-Introduction-Object-Oriented/dp/0131489062">GRASP</a> principles.&nbsp; He calls the LoD Don&#39;t Talk to Strangers.</p><p><strong>Apprentice</strong>:&nbsp; Okay, will do.</p><p><strong>Journeyman</strong>:&nbsp; Have you been following&nbsp;<a href="http://en.wikipedia.org/wiki/Test-driven_development">test driven development</a> (TDD)?</p><p><strong>Apprentice</strong>:&nbsp; Uh..., well..., not really.&nbsp; But I do&nbsp;write the tests afterwards.</p><p><strong>Journeyman</strong>:&nbsp; Complex test fixtures jump right out at you when you&#39;re doing TDD.&nbsp; You realize something is wrong right away.&nbsp; In fact, many times your testing style changes into more of an <a href="http://benpryor.com/blog/index.php?/archives/28-State-based-vs.-Interaction-based-Unit-Testing.html">interaction based style</a>.&nbsp; But I&#39;m getting ahead of myself.&nbsp; Let&#39;s refactor this together.</p><p><strong>Apprentice</strong>:&nbsp; Okay, cool,&nbsp;pair programming.</p><p><strong>Journeyman</strong>:&nbsp; Right.&nbsp; Remember, Order should only deal with OrderLines.</p><p>Now, the setUp() method looks like:</p><pre>     @Before<br />     public void setUp() throws Exception {<br />         OrderLine line1 = stubOrderLineGetPriceToReturn(22.00);<br />         OrderLine line2 = stubOrderLineGetPriceToReturn(66.00);<br /> <br />         OrderLine[] lines = new OrderLine[] {line1, line2};<br />         order = new Order(Arrays.asList(lines));<br />     }<br /> </pre><p>And the Order.getPrice() method looks like:</p><pre>    public double getPrice() {<br />         double price = 0.00;<br />         <br />         for (OrderLine orderLine : orderLines) {<br />             price += orderLine.getPrice();<br />         }<br />         <br />         return price;<br />     }<br /> </pre><p><strong>Apprentice</strong>:&nbsp; Okay, now I really see.&nbsp;&nbsp;Order and its test are simpler, but don&#39;t we have the same amount of work anyway?&nbsp; We had to write OrderLine.getPrice() and test that.</p><p><strong>Journeyman</strong>:&nbsp; Yes.&nbsp; The work is distributed. &nbsp;But I prefer lots of simpler objects with simpler test specifications than more complex objects and tests.</p><p><strong>Apprentice</strong>:&nbsp; What do you mean, more objects?&nbsp; We didn&#39;t create any new objects.</p><p><strong>Journeyman</strong>:&nbsp; I meant generally speaking, not necessarily in this case.&nbsp; Like I said, do a little work and pass the buck.&nbsp; That might mean creating <a href="http://martinfowler.com/bliki/DataClump.html">Data Clumps</a>, aggregate objects for collections, objects that represent abstract data types, and so on, but again, I&#39;m getting ahead of myself.</p><p><strong>Apprentice</strong>:&nbsp; Should I always follow the LoD?</p><p><strong>Journeyman</strong>:&nbsp; Well, there are of course consequences.&nbsp; Classes tend to have larger APIs because sometimes they need to wrap the functionality of collaborating objects.&nbsp; And you need to step through more objects to understand an algorithm as a whole because it&#39;s distributed.&nbsp; But most of the time, I follow LoD because dependencies are reduced.</p><p><strong>Apprentice</strong>:&nbsp; All right, I&#39;ll give it a shot.&nbsp; Thanks.</p><p>&nbsp;</p>]]></description><category>testing</category><category>design</category><category>lod</category></item><item><title>Parallel JUnit Ant Task</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/parallel_junit_ant_task.htm</guid><link>http://clintshank.javadevelopersjournal.com/parallel_junit_ant_task.htm</link><pubDate>Tue, 16 Oct 2007 15:41:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=parallel%5Fjunit%5Fant%5Ftask</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>Over time, how do you maintain a maximum <a href="http://www.think-box.co.uk/blog/2006/02/ten-minute-build-continuous.html">10 minute build</a>? This is an important agile practice for <a href="http://en.wikipedia.org/wiki/Continuous_integration">continuous integration</a>.</p><p>It&#39;s inevitable. As more and more features are added to an application, the code base grows. The build has more to do: more compiling, more tests and metrics to run, more reports to generate. We&#39;ve done numerous things to keep the build time down. In this entry, I&#39;d like to describe one of them: running standard JUnit tests concurrently.</p><p>A colleague of mine came up with this idea.&nbsp;He first did a preliminary search to see if anything already existed.&nbsp; Pretty much everything he found was intrusive, e.g., requiring extending a specialized TestCase.&nbsp; He then&nbsp;attempted&nbsp;to combine <a href="http://ant.apache.org/manual/index.html">Ant&#39;s Parallel and JUnit tasks</a>. That is, he could simply nest N number of JUnit tasks inside a Parallel task. The problem here was coming up with a good way to divide the tests up into equal parts beforehand. My colleague quickly scrapped this idea in favor of a custom Ant task that wrapped the JUnit task.</p><p>The thought was to leverage the JUnit task as much as possible, but fronting it with the ability to run tests in parallel. The conceptual design looks like this:</p><p><img src="http://files.blog-city.com/files/J06/154001/p/f/paralleljunit.png" alt="Conceptual design" width="314" height="74" /></p><p>There are N number of TestExecutors. A TestExecutor runs in a single JVM. Thus, there are N JVMs. Say we have a dedicated, 4 processor integration machine running builds. We may choose to configure our custom task with 5 TestExecutors (we&#39;ll add one&nbsp;assuming&nbsp;we&#39;re not 100% compute bound). Note: if you&#39;re familiar with the JUnit task, you may be wondering how its fork/forkmode attributes fit in. The answer is that they are&nbsp;eliminated in favor of this jvm-count attribute.</p><p>There&#39;s a single TestProvider. His job is to gather all the tests and hand them out one at a time to whichever TestExecutor is ready. A simple protocol exists to let a TestExecutor tell the TestProvider that he&#39;s done and ready for another test.&nbsp; This should maximize parallelism.</p><p>The biggest negative with this approach is that since all tests run in a fixed number of JVMs,&nbsp;there&#39;s some loss of isolation here. Class level/global state set from a previous test could affect later tests. We&#39;re willing to trade this for increased performance.</p><p>The other possible downside is the use of a non-standard Ant task.</p><p>Using this approach&nbsp;we&#39;ve successfully reduced our build time to usable levels.</p><p>&nbsp;</p>]]></description><category>testing</category><category>tools</category><category>build</category><category>ci</category></item><item><title>The todo Test Category</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/todo_test_category.htm</guid><link>http://clintshank.javadevelopersjournal.com/todo_test_category.htm</link><pubDate>Fri, 26 Jan 2007 14:33:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=todo%5Ftest%5Fcategory</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[Recently, <a href="http://www.elharo.com/">Elliotte Rusty Harold</a>  <a href="http://cafe.elharo.com/testing/go-ahead-break-the-build/">blogged</a>  about committing test cases for bugs to the build cycle before fixing them right away.  He enumerated several good reasons for doing this such as the fix being difficult and time consuming.  His point was that breaking the build (where breaking the build includes passing all the unit tests) is okay in some cases.<p>I disagree with this stance, but I think Harold has a really good idea there, which I&#39;ll get to in a moment.  I think breaking the build does include passing all the unit tests and should be part of the continuous integration process.  I&#39;ve always liked the idea of the tests serving as a safety net when I add new functionality or refactor existing code.  The tests not only specify the desired behavior, but act as a regression suite.</p><p>So what&#39;s the idea?  I first learned of the concept of <a href="http://www.ibm.com/developerworks/java/library/j-cq10316/">test categorization</a>   from Cedric Beust&#39;s <a href="http://testng.org/">TestNG</a>  framework and the writings of disco king <a href="http://thediscoblog.com/">Andrew Glover</a> .  Essentially, the concept is to break tests into groups such as unit, component, and system and run them at different intervals.  Unit tests are run many times a day as code is developed, component tests less frequently, and system tests the least frequently of all.  You can categorize tests in several different ways such as by file naming conventions, placing them in different directories, or using Java annotations.</p><p>Harold brings up a new category - the todo category.  (I was originally thinking the fixme category, but I thought todo could serve more purposes.)  The todo category contains all tests that serve as a reminder of work to do.  They obviously fail or there would be nothing to do.  When it&#39;s finally time, the test moves out of the todo category and into the unit, component, or system category as appropriate.  Although you could have a todo category for each of the original, regression categories (todoUnit, todoComponent, and todoSystem), I think that would be overkill.  Notice that I slipped the word regression into &quot;original categories&quot; above.  I&#39;m advocating that the original categories should pass at all times - they should break the build if they fail (well, at a minimum the unit category).  Once tests get into the regression categories, they never go back to todo.</p><p>The obvious downside of this approach and of test categorization in general is the added complexity.  It&#39;s more work to maintain categories and in this case, move tests into different categories than a simple one category fits all approach.  Also, there&#39;s more work (at least initially) to setup the running of those categories.  As always, I recommend weighing the options and deciding what&#39;s best for your particular situation.  See Glover&#39;s writings for more benefits of test categorization.</p>]]></description></item><item><title>Behavior Driven Development with JUnit 4</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/bdd_junit4.htm</guid><link>http://clintshank.javadevelopersjournal.com/bdd_junit4.htm</link><pubDate>Thu, 14 Dec 2006 22:54:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=bdd%5Fjunit4</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>JUnit 4 makes <a href="http://en.wikipedia.org/wiki/Behavior_driven_development">Behavior Driven Development (BDD)</a>  style testing (or specification) easier.</p><p>Let&#39;s quickly look at an example, the idea stolen from one of <a href="http://www.butunclebob.com/ArticleS.DavidChelimsky.SpecOrganization">David Chelimsky&#39;s blog entries</a>  about specifying the behavior of a stack.  We&#39;re focused here on the specification of an empty stack:</p><pre>// notice the class name specifies the context<br />public class EmptyStack {<p>    private Stack stack = null;<br />    <br />    @Before<br />    public void setUp() {<br />        // set up the context<br />        stack = new Stack();<br />    }</p><p>    <br />    // notice the name focuses on the context<br />    @Test<br />    public void shouldBeEmpty() {<br />        assertTrue(&quot;not empty&quot;, stack.isEmpty());<br />    }</p><p>    <br />    @Test(expected=EmptyStackException.class)<br />    public void shouldComplainOnPeek() {<br />        stack.peek();<br />    }<br />    <br />    <br />    // more specification focused methods<br />}</p></pre><p>The point here is that you can get many of the benefits of BDD (a focus on specification rather than testing) using the familiar JUnit framework.</p><p>Now, if you&#39;re a hardcore BDD&#39;er, then you might complain that you still have to use a test-centric vocabulary.  You still need those Test annotations and method calls like assertEquals rather than <a href="http://www.daveastels.com/">Dave Astels&#39;</a>  preferred shouldEquals calls.</p><p>Also, from the legacy side of the fence, you lose the convention of method names starting with testSomething and class names ending with Test.  It&#39;s sometimes hard to let go of that if you&#39;ve been writing tests for a long time and it&#39;s super clear to spot those test methods and test classes if you&#39;re following a naming convention.  Furthermore, Ruby on Rails has taught us that convention is a good thing.  The test method naming convention doesn&#39;t really bother me so much.  The @Test annotation makes things clear enough and shouldSomething really gets us focused on specification.  However, I haven&#39;t let go of ending the class name with Test.  Maybe it&#39;s more because Ant can pick those tests up easier if there&#39;s a standard naming convention.</p><p>The last negative I can think of is that of consistency.  Having a mix of old style test centric tests and BDD style tests could bother some.</p><p>So, let&#39;s be honest.  What am I really doing?  Well, I am incorporating more and more BDD into my work.  However, I still shy away from creating new classes.  The BDD style really lends itself to many test classes (contexts) per class under test.  In the case of Stack, as David Chelimsky points out, you&#39;d also need AlmostEmptyStack, AlmostFullStack, and FullStack classes to fully specify the behavior.  I just can&#39;t commit myself to writing all those.  But I am focusing more on the set up of a context and the specification methods of that context.  I just may cheat a little and combine contexts into a single class.  So perhaps in the Stack example, I&#39;d combine the empty and almost empty stack contexts under one test class.  You know, I&#39;d set up an empty Stack in setUp and for the almost empty context, I&#39;d do a little more set up (push something) in the appropriate shouldSomething methods to make it almost empty.</p><p>I also revert back to legacy style testing sometimes.  This is usually when I&#39;m modifying an existing, old style test.  It&#39;s just simpler and faster, and typically the context isn&#39;t set up too well for the BDD style.</p><p>So my advice is to use the right tool for the job.  Use BDD style when that makes things clearer and you want to focus on specification.  Use test centric style when that&#39;s easier.  Try to do a better job of focusing more on specification and less on verification, especially when you&#39;re adding new behavior.</p><p>&nbsp;</p>]]></description></item><item><title>Testing Private Methods</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/testingprivates.htm</guid><link>http://clintshank.javadevelopersjournal.com/testingprivates.htm</link><pubDate>Sat, 18 Nov 2006 15:37:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=testingprivates</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>How should I test my private helper methods?  Well, I used to just expose the method by making it accessible to the unit test (typically by making it package scoped).</p><p>So if I had a method like this:</p> <pre>    private void someHelperMethod() {<br />    } </pre> <p>I&#39;d expose it like this:</p> <pre>    /** Exposed for testing purposes only. */<br />    void someHelperMethod() {<br />    } </pre> <p>I&#39;d laugh at people who used the &lt;code&gt;PrivateAccessor&lt;/code&gt; of the <a href="http://sourceforge.net/projects/junit-addons">JUnit-addons</a>  project or directly used reflection to get the method and do a &lt;code&gt;method.setAccessible(true)&lt;/code&gt;.  I&#39;d think, &quot;What&#39;s the big deal?  Just change the access of the method.  Then, you&#39;ll make things easier for your IDE when you want to do a method rename refactoring or just find references to this method.&quot;</p><p>I&#39;ve recently hopped the fence and I&#39;m now campaigning for the other side.</p><p>Now, before I get into the why, I should point out that red flags should have gone up after you read the first question of this entry.  Testing private methods?  Why would you do that?  Believe me, as I&#39;ve mentioned <a href="/testbreaks.htm">here</a> , I think unit tests should focus on black box testing as much as is practically possible and therefore, you shouldn&#39;t be testing private methods.  I&#39;m also familiar with the argument, &quot;You could perhaps move those private methods to a helper class and therefore, they wouldn&#39;t be private.&quot;  But I still believe there are cases where it makes sense to (have and) test private methods.  And in this blog, I&#39;m talking about these exceptional cases.</p><p>So what are some exceptional cases?  Think of a high level method specifying the steps of an algorithm or a method on a mediator/controller-like class collaborating with multiple objects.  You did break that method up into smaller, easier to understand, more communicative methods as described <a href="/smallmethods.htm">here</a> , didn&#39;t you?  So which is easier: setting up the object under test and its collaborating objects (the fixture) and calling the private helper method directly; or setting up the fixture and calling the public, higher level method that does some processing before calling the private helper method?  In the latter case, you surely had to do more set up to navigate down to the same entry point in the private method.  If you didn&#39;t or it wasn&#39;t much more work, then I&#39;m talking about a different context.</p><p>All right, so why the switch?  Well, first off, the real intent is to make them private.  Private leaves no doubt that the method isn&#39;t meant to be used outside the class.</p><p>Along with that is encapsulation.  As multiprocessor systems become more mainstream, exploiting concurrency by multi-threading adds complexity to software development.  Encapsulating as much as possible can only simplify reasoning about thread safety.</p><p>One final reason is that making things private helps static analysis tools like <a href="http://findbugs.sourceforge.net/">FindBugs</a>  and <a href="http://pmd.sourceforge.net/">PMD</a>  better identify dead code that can be eliminated.  &quot;Hey, this method is private and it&#39;s not being called!&quot;  Keeping the code base lean and clean is a key to maintainability.</p><p>The biggest downside, as I&#39;ve hinted at above, is doing things like renaming/changing the signature of the method, or just finding references to it.  The IDE won&#39;t make the appropriate changes to the test or find the reference in the test.  But you&#39;ll find this pretty quickly when you run the test and it fails.  Hint: Maybe you should have changed the test first anyway.</p><p>So join me on this side and remember that testing privates is better meant for special cases.</p><p>&nbsp;</p>]]></description></item><item><title>Comments on The Danger of Mock Objects</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/commentsonmocks.htm</guid><link>http://clintshank.javadevelopersjournal.com/commentsonmocks.htm</link><pubDate>Sun, 24 Sep 2006 16:13:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=commentsonmocks</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>Rarely do I disagree with Uncle Bob.&nbsp; I&rsquo;ve learned a lot from reading his writings over the years.&nbsp; He recently blogged about <a href="http://www.butunclebob.com/ArticleS.UncleBob.TheDangerOfMockObjects">mock objects</a>  in response to a <a href="http://beust.com/weblog/archives/000410.html">blog</a>  from Cedric Beust.&nbsp; Check it out, read my comments on Uncle Bob&rsquo;s blog, and decide for yourself.</p><p>&nbsp;</p>]]></description></item><item><title>Checklist for Finding Test Cases</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/testcasechecklist.htm</guid><link>http://clintshank.javadevelopersjournal.com/testcasechecklist.htm</link><pubDate>Sun, 04 Jun 2006 15:19:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=testcasechecklist</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>Suppose I'm focused on specifying the behavior of a particular method.  How do I know if I've specified everything?  I'd like to share a checklist of things I think about to help accomplish this.  The goal is to thoroughly specify the behavior of the method by building up a collection of tests following <a href="http://en.wikipedia.org/wiki/Test_driven_development">test-driven development</a>.</p>
<p>I'm going to base my example on <a href="http://www.springframework.org/">Spring</a>'s <font face="Courier New">BeanFactory</font> interface.  Suppose I'm developing a <font face="Courier New">Map</font> backed <font face="Courier New">BeanFactory</font> and I am currently focused on implementing the following method of the <font face="Courier New">BeanFactory</font> interface:</p>
<pre>Object getBean(String beanName) throws BeansException</pre>
<p>Let's call the class I'm developing <font face="Courier New">MapBackedBeanFactory</font>.  When <font face="Courier New">getBean()</font> is called, the implementation will look up the bean in its <font face="Courier New">beanMap</font> field.</p>
<p>When I think about test cases, I think about:</p>
<ol>
    <li>The state of the object under test (each field), i.e., the fixture.  In the example, this would be the <font face="Courier New">beanMap</font>.<br /></li>
    <li>The state of the parameters of the method I'm calling on the object under test.  In the example, there's only one: the <font face="Courier New">beanName</font>.</li>
</ol>
<p>That was pretty obvious.  Who doesn't think about these things?  But what is the checklist?  The checklist consists of thinking about the following for <strong>each</strong> of the above:</p>
<ol>
    <li><u>A good value</u>.  This will execute the normal flow and is the most obvious test.  For <font face="Courier New">beanMap</font>, this means the <font face="Courier New">beanMap</font> will have a good bean mapping.  For the <font face="Courier New">beanName</font>, a good value is a name that will be found in the <font face="Courier New">beanMap</font>.  There may be multiple good values, not really in this case, but think about the classic bowling game.  You'd want tests for a non-mark frame, a spare, and a strike.  I'll also consider multiple good values for things like collections, maps, and arrays.  That is, maybe I'm dealing with a&nbsp; <font face="Courier New">List</font> and I need a <font face="Courier New">List</font> of two good things.  However, I find that most times, one good value in the collection, map, or array is sufficient.&nbsp; This is because I write the iteration code for the single case and a multiple collection test case wouldn't make me write any new code.&nbsp; If I find I need multiple good values, I'll start with the simple cases and work towards the harder.</li>
    <li><u>A negative case</u>.  In #1, I set up the fixture and parameters to get a good result.&nbsp; Now, I want to think of the negative side.&nbsp; So I found beans in #1, but now I want to <strong>not</strong> find a bean.&nbsp; Pass in an unknown <font face="Courier New">beanName</font>.</li>
    <li><u>A null value</u>.  What if the <font face="Courier New">beanMap</font> were null?  That's probably not realistic, so I'm going to skip that.  What if the <font face="Courier New">beanName</font> were null?  That's possible and according to the <font face="Courier New">BeanFactory</font> JavaDoc, it looks like the contract specifies to throw a <font face="Courier New">NoSuchBeanDefinitionException</font>.  I better write that test.  Many times, if you don't handle null, you'll get a <font face="Courier New">NullPointerException</font> and that might be acceptable.  In those cases, I wouldn't write a test (since it wouldn't require any code).</li>
    <li><u>An empty value</u>.  The <font face="Courier New">beanMap</font> could have no mappings or the <font face="Courier New">beanName</font> could be <font face="Courier New">&quot;&quot;</font>.  I think #2 covered us here, so no test is required.</li>
    <li><u>An invalid value</u>.  What if <font face="Courier New">beanMap</font> were invalid?  What if it were a <font face="Courier New">Map</font> of <font face="Courier New">Integer</font>s to bean <font face="Courier New">Object</font>s?  I'm going to assume this is unrealistic in this case.  We would have handled such a situation when the map was loaded.&nbsp; What if the <font face="Courier New">beanName</font> were <font face="Courier New">&quot;invalid&quot;</font>?  Well, we covered that in #2.</li>
    <li><u>An exceptional case</u>.  I'm pretty much talking about handling exceptions here.  Maybe we need to write a test case to see if we handle an exception properly.  The example doesn't have such a case, but suppose the method under test were doing some I/O.  Maybe we'd need to write a test to see if we handled that properly.  In all honesty, even though I think about this, I don't usually write a test to handle exceptions.  I won't get 100% test coverage, but I usually ignore writing the test because checked exceptions force me to write the necessary handling code.</li>
</ol>
<p>That's a lot of stuff to think about for each object field/parameter.  That's the biggest negative - thinking about all those things, all the possibilities, all the time it takes.  Many times, as you saw in the example, most of those things just aren't realistic.</p>
<p>  Sometimes I'll take a different approach.  I'll start with the simple, good case.  I'll write the test and get it to pass by writing the minimal code to do so.&nbsp; In the example, I'd have:</p>
<pre>return beanMap.get(beanName);</pre>
<p>  Then, I'll look at the method I just wrote <strong>word by word</strong> (or maybe token by token).&nbsp; At each word, I'll think about special cases (things like in the checklist) and jot down test cases.  Then, I'll go back to the test, write one of the jotted-down tests, get it to pass, and continue until I'm done.  It's a different perspective that goes a little faster at the risk of not quite catching everything as the first approach.&nbsp; I like this second approach and have been doing it more and more lately.<br /></p>
<p><br /></p>]]></description></item><item><title>Test Smell: Test Breaks After Good Refactoring</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/testbreaks.htm</guid><link>http://clintshank.javadevelopersjournal.com/testbreaks.htm</link><pubDate>Fri, 17 Mar 2006 21:53:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=testbreaks</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>Goal: Write a thorough unit test that doesn&rsquo;t require any changes after mercilessly refactoring the object it&rsquo;s testing.</p>
<p>I&rsquo;ve been noticing that I sometimes feel a need to go back and modify my test to be more in line with the object it&rsquo;s testing.  And I&rsquo;m not alone.  David Chelimsky recently wrote a related <a href="http://butunclebob.com/ArticleS.DavidChelimsky.KeepingInfectionInCheck">blog</a>.  That blog, in the context of my goal, focuses more on changing tests because they start drifting away from the code they&rsquo;re testing.</p>
<p>In this blog, I want to talk about changes required because the tests break as a result of good refactoring.  This isn&rsquo;t something that occurs often, but it does happen, and I think it&rsquo;s a code smell.  One of the main advantages of having a test suite is that you can refactor to make the code better or try a different algorithm without worrying whether the final result broke existing functionality.  When the tests break as a result of correct refactoring, I lose that safety net kind of feeling.</p>
<p>So what does it mean when a test breaks as a result of good refactoring?  Well, what jumps out is that the test is too coupled to the main object it&rsquo;s testing.  However, it is a unit test and unit tests are traditionally white box tests and white box tests imply knowledge of the code and that implies coupling.</p>
<p>What about a black box approach to unit testing to reduce coupling?  Elliotte Rusty Harold recently posted a comment on one of his blogs <a href="http://cafe.elharo.com/java/experimental-programming/">Experimental Programming</a>, &ldquo;The class is a black box which is accessed solely through its public interface. The internal details are deliberately opaque, and thus can be changed as necessary to improve performance or other desirable characteristics without breaking client code.&rdquo;<br /> </p>
<p>Yeah, that&rsquo;s what I&rsquo;m talking about, but I think a total black box approach would be too coarse grained, particularly for ensuring 100% test coverage.  Specifically, I&rsquo;m thinking that touching every conditional statement would require a lot of black box test code.  I think the first tip then is to find a balance between black box and white box testing; the sub-goal being to find the appropriate coupling to get 100% coverage with the least amount of test code.  I&rsquo;m trying to keep this in mind when I write tests.</p>
<p>Another technique to help achieve the goal is to shy away from mock objects, particularly strict mock objects.  Martin Fowler wrote an article called &ldquo;<a href="http://www.martinfowler.com/articles/mocksArentStubs.html">Mocks Aren&rsquo;t Stubs</a>&rdquo;.  In it, he describes the difference between mock objects and stubs.  He also describes two styles of testing: state-based and interaction-based.  Interaction-based testing uses lots of mocks, which really couples tests to the implementation details.  This is not what I want if I want to achieve my goal.  So I prefer the state-based approach Martin talks about.</p>
<p>Now, having said that, I do use mocks in certain situations.  They&rsquo;re good at making sure your object made a call on a secondary object:</p>
<pre>   public void testInitRegisters() {<br />&nbsp;&nbsp;&nbsp;   // setup a mock object that expects a register call<br /> &nbsp;&nbsp;   // and inject it into the object under test<br />&nbsp;&nbsp;&nbsp;   objectUnderTest.init();<br />&nbsp;&nbsp;&nbsp;   // verify the mock<br />   }<br /></pre>
<p>Typically, I find that I use mock objects when I&rsquo;m testing <font face="Courier New">void</font> methods, just like the <font face="Courier New">init()</font> method above.  In fact, thinking more about it, it might actually be when the method I need to call on the object I&rsquo;m mocking is <font face="Courier New">void</font>, just like <font face="Courier New">register()</font> method above.  I&rsquo;ll have to pay more attention, but that seems to make sense.  The point is to use mock objects only when they are more practical than the state-based approach, preferring the state-based approach otherwise to reduce test coupling.</p>
<p>Stubs are great for tests whose primary object calls methods that return values from secondary objects.  I use mock libraries to create stubs, particularly for Interfaces and expensive-to-create classes, since it&rsquo;s so easy to do so.  I reduce test coupling by creating lenient stub objects that either ignore unexpected method calls or return empty values (<font face="Courier New">0</font>, <font face="Courier New">null</font>, <font face="Courier New">false</font>) for unexpected method calls.  In <a href="http://www.easymock.org/">EasyMock</a> terms, I&rsquo;m talking about nice controls.</p>
<p>I also use the <a href="http://www.xpuniverse.com/2001/pdfs/Testing03.pdf">Object Mother</a> pattern as a factory to create the stub objects.  This is so that the stub creation code can be shared among many test classes.  I pass the values I want returned into object mother.  The object mother creates the stub object with those values.  In this way, the test has all the knowledge of the canned results.  Then I use a state-based approach to verify everything.  Here&rsquo;s a quick example, putting this all together:</p>
<pre>   private Order order = new Order();<br /></pre>
<pre>   public void testGetTotal() {<br />&nbsp;     // LineItem is an interface.  LineItemMother uses a<br />     &nbsp;// mock library for creating a stub LineItem<br />&nbsp;     LineItem lineItem = LineItemMother.newItem(3, 2.50);<br />      order.add(lineItem);<br />      assertEquals(7.50, order.getTotal(), 0.001);<br />   }<br /></pre>
<p>&nbsp;</p>]]></description></item><item><title>A Common Set of Test Refactorings</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/commontestrefactorings.htm</guid><link>http://clintshank.javadevelopersjournal.com/commontestrefactorings.htm</link><pubDate>Sun, 19 Feb 2006 13:02:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=commontestrefactorings</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[Today, I want to blog about what I think might be my most common set of low level Eclipse refactorings used together when I&rsquo;m developing unit tests.&nbsp;I use this set to parameterize tests as discussed in <a href="http://clintshank.javadevelopersjournal.com/tabulartests.htm">Tabular Tests</a>.&nbsp;Lately, I&rsquo;ve been trying to use the Eclipse refactorings as much as possible, thinking that the automation would be less likely to break things than if I made the changes manually.&nbsp;It&rsquo;s also interesting to make code changes without doing any typing.&nbsp;The set consists of:
<ol type="1" style="margin-top: 0in;" start="1">
    <li><font face="Courier New">Extract      Local Variable</font> (possibly multiple times).</li>
    <li><font face="Courier New">Extract      Method</font></li>
    <li><font face="Courier New">Inline</font>      the local variables created in step 1.</li>
</ol>
So let&rsquo;s suppose I&rsquo;m about to start work on a new method.&nbsp;I like to start with a brain dump of most, if not all of the test cases.&nbsp;This is to get me thinking about the desired behavior and also because it clears my head, so that when I start writing the code, I can focus.&nbsp;Once I&rsquo;m ready, I start with a simple case.
<p>Let&rsquo;s take the <font face="Courier New">Range.equals()</font> example from the previous blog.&nbsp;My brain dump would have resulted in a table like this:</p>
<p>
<table width="200" cellspacing="1" cellpadding="1" border="1" align="">
    <tbody>
        <tr>
            <td><strong>Min</strong></td>
            <td><strong>Max</strong></td>
            <td><strong>Equals?</strong></td>
        </tr>
        <tr>
            <td>same</td>
            <td>same</td>
            <td>true</td>
        </tr>
        <tr>
            <td>different</td>
            <td>same</td>
            <td>false</td>
        </tr>
        <tr>
            <td>same</td>
            <td>different</td>
            <td>false</td>
        </tr>
    </tbody>
</table>
</p>
<p>Yes, I would have jotted down other, exceptional cases for things like <font face="Courier New">null</font> and a non-<font face="Courier New">Range</font> object, but that&rsquo;s out of scope here.&nbsp;For this blog, I&rsquo;m just concentrated on comparing <font face="Courier New">Range</font> objects.&nbsp;I start with the positive case and come up with something like:</p>
<p><code><span style="font-size: 10pt;">&nbsp;&nbsp; private static final int MIN = 1;<br /> &nbsp;&nbsp; private static final int MAX = 7;<br /> <br /> &nbsp;&nbsp; private Range range = new Range(MIN, MAX);</span></code> </p>
<div><code>&nbsp;</code></div>
<code><span style="font-size: 10pt;">&nbsp; &nbsp;public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; Range anotherRange = new Range(MIN, MAX);<br /> &nbsp; &nbsp; &nbsp; assertEquals(range, anotherRange);<br /> &nbsp; &nbsp;}</span></code>
<p>&nbsp;</p>
<p><code><span style="font-size: 10pt;"> </span></code>To be honest, I probably would have had the <font face="Courier New">MIN</font> and <font face="Courier New">MAX</font> constants hardcoded in there, but at some point, I would have performed <font face="Courier New">Extract Constant</font> to come up with the above.</p>
<p>After I get that test to pass (by returning <font face="Courier New">true</font>), I&rsquo;m ready to write the next test.&nbsp;I want to pick something simple again (of course in this example they&rsquo;re all simple).&nbsp;But before I write the next test, I do something that is probably considered cheating by the hardcore test-driven development crowd.&nbsp;I&rsquo;m not too concerned because I know from experience that I&rsquo;m going to end up with tabular tests.&nbsp;In fact, the table is specified above! &nbsp;So here goes the set of refactorings:</p>
<p>I first <font face="Courier New">Extract Local Variable</font> on <font face="Courier New">MIN</font> and <font face="Courier New">MAX</font>:</p>
<p><code><span style="font-size: 10pt;">&nbsp; &nbsp;public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; <strong>int testMin = MIN;<br /> &nbsp; &nbsp; &nbsp; int testMax = MAX;</strong><br /> &nbsp; &nbsp; &nbsp; Range anotherRange = new Range(<strong>testMin, testMax</strong>);<br /> &nbsp; &nbsp; &nbsp; assertEquals(range, anotherRange);<br /> &nbsp; &nbsp;}</span></code></p>
<p><code><span style="font-size: 10pt;"></span></code><span style=""><br /> </span>Now I know from my table that I need a <font face="Courier New">boolean</font> for my expected equals. There's no simple Eclipse refactoring so I manually make the change: </p>
<p><code><span style="font-size: 10pt;">&nbsp;&nbsp; public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; int testMin = MIN;<br /> &nbsp; &nbsp; &nbsp; int testMax = MAX;<br /> &nbsp; &nbsp; &nbsp; <strong>boolean expectedEquals = true;</strong><br /> &nbsp; &nbsp; &nbsp; Range anotherRange = new Range(testMin, testMax);<br /> &nbsp; &nbsp; &nbsp; <strong>assertEquals(expectedEquals, range.equals(anotherRange));</strong><br /> &nbsp; &nbsp;}</span></code></p>
<p><code><span style="font-size: 10pt;"></span></code><span style=""><br /> </span>I highlight the last two lines and perform <font face="Courier New">Extract Method</font>:<br /><br /> <code><span style="font-size: 10pt;">&nbsp; &nbsp;public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; &nbsp;int testMin = MIN;<br /> &nbsp; &nbsp; &nbsp; &nbsp;int testMax = MAX;<br /> &nbsp; &nbsp; &nbsp; &nbsp;boolean expectedEquals = true;<br /> &nbsp; &nbsp; &nbsp; &nbsp;<strong>testEqualsAnotherRange(testMin, testMax, expectedEquals);</strong><br /> &nbsp; &nbsp;}<br /> <br /><strong> &nbsp; &nbsp;private void testEqualsAnotherRange(int testMin, int testMax,<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean expectedEquals) {<br /> &nbsp; &nbsp; &nbsp; &nbsp;Range anotherRange = new Range(testMin, testMax);<br /> &nbsp; &nbsp; &nbsp; &nbsp;assertEquals(expectedEquals, range.equals(anotherRange));<br /> &nbsp; &nbsp;}</strong></span></code></p>
<p><code><span style="font-size: 10pt;"></span></code><span style=""><br /> </span>I want to get back to what it looks like in the table, so I <font face="Courier New">Inline</font> each local variable:</p>
<p><code><span style="font-size: 10pt;">&nbsp;&nbsp; public void testEqualsAnotherRange() {<br />&nbsp; &nbsp; &nbsp; testEqualsAnotherRange(<strong>MIN, MAX, true</strong>);<br /> &nbsp; &nbsp;}<br /> <br /> &nbsp; &nbsp;private void testEqualsAnotherRange(int testMin, int testMax,<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean expectedEquals) {<br /> &nbsp; &nbsp; &nbsp; Range anotherRange = new Range(testMin, testMax);<br /> &nbsp; &nbsp; &nbsp; assertEquals(expectedEquals, range.equals(anotherRange));<br /> &nbsp; &nbsp;}</span></code></p>
<p><code><span style="font-size: 10pt;"></span></code><span style=""><br /> </span>I just created the first row of the table by applying the set of refactorings that is the topic of this blog.&nbsp;At this point, I usually look at the parameterized method and make sure I&rsquo;m happy with the signature.&nbsp;I&rsquo;ve already done most of this when I extracted the method, but Eclipse doesn&rsquo;t let me make the method <font face="Courier New">static</font>, if that&rsquo;s appropriate (in this case it&rsquo;s not).&nbsp;Eclipse also lists all the checked exceptions, which for tests, I&rsquo;d rather generalize to <font face="Courier New">Exception</font>.&nbsp; This doesn't apply in this case either.<br /></p>
<p>During this set of refactorings, I would have ran the tests after a change or two just to make sure I didn&rsquo;t break anything.&nbsp;I also do that because it feels good to get that green bar.</p>
<p>This is a pretty trivial example.&nbsp;Most of the time much more is going on in the parameterized method, but this still shows the removal of duplication.</p>
<p>I could have made these changes manually, but like I mentioned above, I like the automation; I&rsquo;m less likely to break things.</p>
<div>Okay, I can write the next test now:</div>
<p><code><span style="font-size: 10pt;">&nbsp;&nbsp; public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; testEqualsAnotherRange(MIN, &nbsp; &nbsp; MAX, true);<br /> &nbsp; &nbsp; &nbsp; <strong>testEqualsAnotherRange(MIN - 1, MAX, false);</strong><br /> &nbsp; &nbsp;}</span></code></p>
<p><code><span style="font-size: 10pt;"> </span></code>I&rsquo;ve got the red bar.&nbsp;Time to make it green.</p>
<div>&nbsp;</div>]]></description></item><item><title>TDD Pattern: Tabular Tests</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/tabulartests.htm</guid><link>http://clintshank.javadevelopersjournal.com/tabulartests.htm</link><pubDate>Sun, 05 Feb 2006 19:23:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=tabulartests</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p><font face="Arial">Today, I thought I'd blog about one of my most common testing patterns.&nbsp; I call it table-like tests because I end up with a test method that calls a helper, parameterized test method.&nbsp; The high level test method shows all the test cases in what looks kind of like a table.&nbsp; </font>     To give proper credit to this technique, I believe I read about it on <a href="http://www.junit.org/">www.junit.org</a> or one of the XP sites years ago, but I couldn't find the article with a quick Google search.&nbsp; To demonstrate, <font face="Arial">suppose I were working on a <font face="Courier New">Range</font> class and was currently focused on the </font><font face="Arial"><font face="Courier New">equals() </font>method.&nbsp; I'd end up with the following:</font></p>
<font face="Courier New">&nbsp;&nbsp; private static final int MIN = 1;<br />&nbsp;&nbsp; private static final int MAX = 7;<br /><br /> &nbsp;&nbsp; private Range range = new Range(MIN, MAX);<br /><br />&nbsp;&nbsp; public void testEqualsAnotherRange() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testEqualsAnotherRange(MIN,&nbsp;&nbsp;&nbsp;&nbsp; MAX, true);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testEqualsAnotherRange(MIN - 1, MAX, false);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testEqualsAnotherRange(MIN,&nbsp;&nbsp;&nbsp;&nbsp; MAX + 1, false);<br /></font><font face="Courier New"><font face="Courier New">&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp; private void testEqualsAnotherRange(int testMin,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int testMax, boolean expectedEquals) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br />&nbsp;&nbsp; }<br /></font>  <br /></font>
<p><font face="Arial">Now, I know there are other tests for the <font face="Courier New">equals()</font> method, but I'm focusing on the meat of the tests here.&nbsp; I also want to emphasize that I don't start out specifying all the tests like this.&nbsp; I actually write one of the test cases, get that test to pass by minimally implementing the <font face="Courier New">Range</font> class, and then, as I start to write the next test case, I see the pattern and will extract the method and parameterize it for the next test case.</font></p>
<p><font face="Arial"> </font><font face="Arial">Time to make some points:</font></p>
<ol>
    <li>Test cases for the <font face="Courier New">equals()</font> method are grouped together.&nbsp; If I want to know how the <font face="Courier New">equals()</font> method works for non-exceptional cases, all the test cases are right there. When I write code, I focus on a method for a particular production class.&nbsp; That is, I'm trying to build out all the functionality for that method before moving on to the next method (or class).&nbsp; I find it helpful to have all the test cases grouped together.&nbsp; Parameterizing tests keeps the test cases as close together as possible.</li>
    <li>Whenever I detect duplication, I strive to remove it.&nbsp; This is a way to do so among the test cases.</li>
    <li>The mapping between test methods and production class methods is nearly one to one.&nbsp; There's one main <font face="Courier New">testEquals() </font>method for one <font face="Courier New">equals()</font> method.&nbsp; Of course, I'll have test methods for exceptional cases, but I don't have much more test methods than production methods.&nbsp; If I kept each test case in its own method, I'd have a harder time removing duplication and I would have much more test code than production code.<br /></li>
</ol>
<p><font face="Arial"> </font></p>
<p><font face="Courier New"><font face="Arial">I should also point out that many times, I don't use the same fixture.&nbsp; In the example above, I did, but many times I don't.&nbsp; The parameters passed in to the test helper method could be used to setup the production object.&nbsp; Sometimes, the parameters are used to help mock a nested object.&nbsp;&nbsp; Take, for example, the typical web site <font face="Courier New">Order</font> class.&nbsp; Let's suppose I were focused on the </font><font face="Arial"><font face="Courier New">getTotal()</font> method:</font></font></p>
<font face="Courier New"><font face="Courier New">&nbsp;&nbsp; private Order order = new Order();<br /><br />&nbsp;&nbsp; public void testGetTotal() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assertEquals(0.00, order.getTotal(), 0.0001);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testGetTotal(1, 3.00, 3.00);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testGetTotal(3, 5.00, 18.00);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // etc.<br />&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp; private void testGetTotal(int lineItemQuantity,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double lineItemPrice, double expectedTotal) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // create a line item<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add line item to order<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // verify total<br />&nbsp;&nbsp; }<br /></font><br /><font face="Arial"><br /></font></font>
<p><font face="Courier New"><font face="Arial">Now, I should point out some of the concerns of using this technique:</font></font> <br /></p>
<ol>
    <li><font face="Courier New"><font face="Arial">It doesn't follow the JUnit convention of working with the same fixture per <font face="Courier New">TestCase</font>.&nbsp; Many times, my &quot;fixture&quot; is just an empty production object that will be setup in individual test methods.&nbsp; B</font></font><font face="Arial">ehaviour Driven Development (BDD) is getting press these days and its focus is on fixtures called Contexts.&nbsp; Caveat: my knowledge of BDD is from reading this one <a href="http://blog.daveastels.com/files/sdbp2005/BDD%20Intro.pdf">article</a> by Dave Astels, and I haven't experimented with it.&nbsp; It seems to be good for showing behavior in a given context, whereas my technique is method focused.&nbsp; Maybe I'll give it a shot in the future.<br /></font></li>
    <li><font face="Courier New"><font face="Arial">Because there isn't a real fixture, the test methods are more static than object-oriented.&nbsp; That is, I might not be working with instance variables of the <font face="Courier New">TestCase</font>.&nbsp; I don't find this to be an issue though.&nbsp; I thought I'd point it out for OO purists.<br /></font></font></li>
    <li><font face="Courier New"><font face="Arial">Gerard Meszaros, who is writing a good book on <a href="http://tap.testautomationpatterns.com:8080/Book%20Outline.html">test automation patterns</a>, pointed out that if one of my earlier tests fails, then later tests won't be run.&nbsp; He also pointed out that it is harder to determine how many total test cases there are since some are embedded.&nbsp; These are valid points.&nbsp; To counter, I mentioned that I am following test-driven development, so I am building the tests as I go, and I usually don't run into the situation where an earlier test fails, although it does happen.&nbsp; I also never run into the situation where a bunch of tests are all of the sudden failing.&nbsp; As for the number of test cases, I've never had a need to know.&nbsp; But his points are definitely valid.</font></font></li>
    <li><font face="Courier New"><font face="Arial">Tests aren't isolated.&nbsp; You don't start fresh each time.&nbsp; Thus, earlier tests could have unwanted side effects.&nbsp; You have to be aware of this.&nbsp; I either want side effects (as in the <font face="Courier New">Order</font> example where I'm building up an <font face="Courier New">Order</font>) or I simply start with an empty production object and build it up in the helper test method.<br /></font></font></li>
</ol>
<font face="Courier New"><br /></font>]]></description></item></channel></rss>