<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>Agile</title><link>http://www.agileprogrammer.com/oneagilecoder/category/111.aspx</link><description>Writings about agile software development</description><managingEditor>Brian Button</managingEditor><dc:language>en-US</dc:language><generator>.Text Version 0.95.2005.109</generator><item><dc:creator>Brian Button</dc:creator><title>I'm thinking of an example...</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2009/04/03/25035.aspx</link><pubDate>Fri, 03 Apr 2009 18:08:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2009/04/03/25035.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/25035.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2009/04/03/25035.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/25035.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/25035.aspx</trackback:ping><description>&lt;p style="clear: both"&gt;I'm writing an example for a workshop on estimation in an agile context, and I'm considering using the idea of preparing a multi-course meal. I think I like this because it is entirely non-technical, so I can give it to developers, customers, QA, or anyone else without regard to previous technical knowledge. I also like it because there are obvious tie-ins to concepts like freshness, inventory, rapid delivery, and so on. That is as close to the real-world considerations of a software project I can think of while keeping it approachable for everyone. &lt;/p&gt;&lt;p style="clear: both"&gt;So, here goes:&lt;/p&gt;&lt;p style="clear: both"&gt;I have 100 guests showing up in 6 hours for an end of project celebration. This is the entire project team and their families. They did a great job on their last delivery, so we want to reward them with a feast. The menu is top secret, but we want it to surprise and delight them as much as possible. Here is the schedule:&lt;/p&gt;&lt;p style="clear: both"&gt;12:00PM - 1:00PM - Guests start arriving. I need hors d'oeuvres ready and passed around through the crowd as they gather.&lt;/p&gt;&lt;p style="clear: both"&gt;1:00PM-2:00PM - Salad and appetizer course served&lt;/p&gt;&lt;p style="clear: both"&gt;3:00PM-4:00PM - Lunch&lt;/p&gt;&lt;p style="clear: both"&gt;5:00PM-5:30PM - Dessert and Coffee&lt;/p&gt;&lt;p style="clear: both"&gt;That gives us 4 different courses to serve. I have a kitchen filled with supplies, everything you might need. The pantry is well stocked, tons of pots and pans, spices, herbs, three ovens and stoves, and anything else you might need. I just need you and your team of world-class chefs to create something for me and my guests.&lt;/p&gt;&lt;p style="clear: both"&gt;I'd like to have 4 or 5 different hors d'oeuvres, 3 different kinds of salads, 2 kinds of appetizers. Lunch should be a buffet of 3 meats, 2 pasta dishes, 3 vegetables, and a rice dish. I'd like 3 different desserts, one decadently sweet, one fruit dish, and a big cake.&lt;/p&gt;&lt;p style="clear: both"&gt;So, off you go. Give me a selection of great food at each of these times. Bear in mind that things can go wrong, like ovens not working, menus changed, schedules shortening, so you'll have to constantly keep your eye on time and delivering the best culinary value for the given effort.&lt;/p&gt;&lt;p style="clear: both"&gt;So, how did that sound? I intend to mix things up by adding vegetarian dishes to the lunch menu, an oven going out, changing the schedule, running out of something... Basically doing whatever it takes to have them adapt, replan, and go forward.&lt;/p&gt;&lt;p style="clear: both"&gt;Does this sound close enough to a real software project to be useful? Does the exercise sound like anything that would teach estimation and prioritization? Any sort of feedback would be greatly appreciated.&lt;/p&gt;&lt;p style="clear: both"&gt;bab&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;br class='final-break' style='clear: both' /&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/25035.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>Agile in 6 words</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2009/01/13/24842.aspx</link><pubDate>Tue, 13 Jan 2009 16:18:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2009/01/13/24842.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/24842.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2009/01/13/24842.aspx#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/24842.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/24842.aspx</trackback:ping><description>&lt;p&gt;My good friend, &lt;a href="http://fungoes.net/"&gt;Matt Philip&lt;/a&gt;, wrote an &lt;a href="http://stl-sabr.bajink.com/fungoes/?p=1555"&gt;interesting blog entry&lt;/a&gt; the other day about Six-Word Memoirs By Writers Famous and Obscure. I thought it might be interesting to write about the agile methods, their values, and their practices, following a similar style. So here goes, my own take on Six-Word Memoirs on Agile Values, People, and Activities&amp;#8230;  &lt;/p&gt;

&lt;p&gt;Agile: Business plans, developers do, people thrive &lt;br&gt;
Agile: Don&amp;#8217;t want &amp;#8220;resources&amp;#8221;, give me people! &lt;br&gt;
Agile: Project success comes from working together &lt;br&gt;
Agile: Engine converting features to software daily &lt;br&gt;
Agile: Solve biggest problem. Lather, rinse, repeat  &lt;/p&gt;

&lt;p&gt;QA: Hey, I made the team!!! Yea, QA!!! &lt;br&gt;
QA: Building it right the first time &lt;br&gt;
QA: Equal parts developer, tester, and customer &lt;br&gt;
QA: The glue that holds &amp;#8216;em together &lt;br&gt;
QA: Helping customers define what they want  &lt;/p&gt;

&lt;p&gt;Scrum Master: Equal parts facilitator, friend, and psychologist &lt;br&gt;
Scrum Master: My job is to listen carefully &lt;br&gt;
Scrum Master: If I do it, you don&amp;#8217;t! &lt;br&gt;
Scrum Master: Fixing problems is not the solution  &lt;/p&gt;

&lt;p&gt;Customer: Content is mine, method is yours &lt;br&gt;
Customer: I chart the course to success &lt;br&gt;
Customer: You let me worry about that &lt;br&gt;
Customer: Word of mouth, my best tool &lt;br&gt;
Customer: Don&amp;#8217;t read specs, ask me questions  &lt;/p&gt;

&lt;p&gt;Developer: I eat user stories for breakfast &lt;br&gt;
Developer: I trust my customer and team &lt;br&gt;
Developer: It feels good to be valued &lt;br&gt;
Developer: Method is mine, content is yours &lt;br&gt;
Developer: Best architectures owned by whole team &lt;br&gt;
Developer: Ivory tower architects need not apply &lt;br&gt;
Developer: Red, green, refactor. Lather, rinse, repeat. &lt;br&gt;
Developer: Its more fun done in pairs &lt;/p&gt;

&lt;p&gt;Delivery: Running, tested features - what really counts &lt;br&gt;
Delivery: Scope is negotiable, quality is not &lt;br&gt;
Delivery: Not over, under promising, just delivering &lt;br&gt;
Delivery: Reliably delighting the customer every week  &lt;/p&gt;

&lt;p&gt;I could do more, but lets leave it there. Any more?&lt;/p&gt;

&lt;p&gt;&amp;#8212;bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/24842.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>Obvious comment of the day - TDD makes Pair Programming easier</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2008/10/08/24654.aspx</link><pubDate>Wed, 08 Oct 2008 13:06:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2008/10/08/24654.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/24654.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2008/10/08/24654.aspx#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/24654.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/24654.aspx</trackback:ping><description>&lt;p&gt;A fairly obvious observation hit me today&amp;#8230;&lt;/p&gt;

&lt;p&gt;If you are trying to pair program without also doing test driven development, when do you change roles? When doing TDD with Pairing, there is a rhythm to when the roles switch - see &lt;a href="http://www.peterprovost.org/blog/post/Micro-Pairing-Defined.aspx"&gt;Micro Pairing&lt;/a&gt;. But if you&amp;#8217;re not doing TDD, the person typing is frequently lost in solving a fairly large problem, they are balancing a bunch of things in their heads, and they have to finish a big thought before they could possibly swap the keyboard with their pair. So, while the typer is solving these big problems, what does the other person do? Just sit there? It just seems pretty painful&amp;#8230;&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m sure its not impossible, but it sure seems like TDD is a near necessity for Pair Programming.&lt;/p&gt;

&lt;p&gt;Thoughts?&lt;/p&gt;

&lt;p&gt;&amp;#8212; bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/24654.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>A Story Splitting Story</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2008/10/02/24641.aspx</link><pubDate>Thu, 02 Oct 2008 09:29:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2008/10/02/24641.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/24641.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2008/10/02/24641.aspx#Feedback</comments><slash:comments>12</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/24641.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/24641.aspx</trackback:ping><description>&lt;p&gt;This is a true story from a true client I&amp;#8217;m working with . The names and details have been changed to protect the innocent&amp;#8230;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Story splitting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Several of us were attending a pre-sprint planning meeting yesterday, trying to flesh out the user stories that the product owner was planning to bring to sprint planning in a few days. They are still pretty early in their agile adoption as well as their technology adoption, so there are lots of questions floating around about process and tools.&lt;/p&gt;

&lt;p&gt;A story came up in the meeting that described the interaction that a user would take when visiting the site the first time, more specifically around the user agreement presented to them the first time they logged into the site. The story was something like:&lt;/p&gt;

&lt;p&gt;&amp;#8220;As a user, I want to be to able to accept the user agreement so that I can access my content&amp;#8221;&lt;/p&gt;

&lt;p&gt;The details of this story included a specific workflow to follow if the user didn&amp;#8217;t agree, including errors to show them, and a need to refrain from showing the user agreement again after agreeing to it the first time. &lt;/p&gt;

&lt;p&gt;Conversation around this story went on for a while, mainly focusing around the second part, how to handle remembering what the user did the last time they visited the site. There were technical details about where this information needed to be stored that hadn&amp;#8217;t been agreed to yet, and team spun a bit around this issue.&lt;/p&gt;

&lt;p&gt;The suggestion was made to split the story into two. The first story would be the same as the first, &amp;#8220;As a user, I want to be able to accept the user agreement so that I can access my content&amp;#8221;, and it included all the acceptance criteria other than the remembering part. The second story would be &amp;#8220;As a previously logged in user, I will not be presented with the user agreement, since I had already agreed to it&amp;#8221;, which picked up the remembering part. &lt;/p&gt;

&lt;p&gt;By splitting the story in this way, they were able to work out the important part for the customer, which was the user agreement workflow, and defer the technical issues over where to store the agreement status until they could discuss the issue more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Moral of the story&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When discussing a story, if there is contention about how part of it is done, it is sometimes possible to split the story so that the understood part is separated from the not understood part, allowing progress to be made. In this case, we knew how to write the workflow, but not how to prevent the user from seeing the agreement each time. We split the story at that particular edge, which allowed the team to build something useful, and to defer what they didn&amp;#8217;t know yet until they knew more about it later.&lt;/p&gt;

&lt;p&gt;&amp;#8212; bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/24641.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>What do you think of this code?</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2008/01/13/23995.aspx</link><pubDate>Sun, 13 Jan 2008 21:15:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2008/01/13/23995.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/23995.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2008/01/13/23995.aspx#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/23995.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/23995.aspx</trackback:ping><description>&lt;p&gt;I recently finished 6 weeks of coding for a client, and it was heaven! I actually got a chance to code every day, for 6 solid weeks. It was a chance for me to learn C# 3.0, and a chance to work on testing things that are hard to test. It was great!&lt;/p&gt; &lt;p&gt;Out of the work, came several interesting observations and coding techniques, all rooted in C# 3.0. Since no one at work has any experience with these new idioms I "invented", "discovered", or just "copied", I'd love to get some reader feedback. I'll start with this one trick I tried, and follow on with more as the mood strikes me over time.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Trick 1: Using extension methods and a marker interface in place of implementation inheritance&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;I had an instance of code duplication in two parallel hierarchies of classes, and I wanted to find a way to share the code. One option would be to use inheritance, factoring out another base class above BaseResponse and BaseRequest. This is where methods common to requests and responses could both live. Using inheritance as a way to reuse code in a single inheritance language is a pretty heavyweight thing to do. I'd rather find a way to use delegation, since that preserves the SRP in my class hierarchy. Instead, I decided to try an extension method, and just use that method where I needed it. To avoid polluting Object with unnecessary methods, however, I came up with the idea of using a marker interface on the classes I wanted to have these extension methods, limiting the scope where these extra methods were visible. (No idea if anyone else has done this yet or not)&lt;/p&gt; &lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="296" alt="ClassDiagram1" src="http://www.agilestl.com/BlogImages/ClassDiagram1.jpg" width="647" border="0"&gt; &lt;/p&gt; &lt;p&gt;For each request and response class, in the two parallel&amp;nbsp; hierarchies, my client requirements made it necessary to add an XmlRoot attribute to tell the XmlSerializer that this object was the root of an XML document and to specify the runtime name of this element. To let me get the runtime name of each request and response object, for auditing and logging purposes, both hierarchies had a CommandName property, containing the exact same code. This was the code in question that I was trying to share.&lt;/p&gt; &lt;p&gt;As a simple exercise, I created an extension method to deal with this:&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; SssMessageExtensionMethods
    {
        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; GetCommandNameFromXmlRootAttribute(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; object message)
        {
            &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[] attributes = message.GetType().GetCustomAttributes(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(XmlRootAttribute), &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;);
            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (attributes.Length == 0) &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; message.GetType().Name;

            XmlRootAttribute xmlRootAttribute = attributes[0] &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; XmlRootAttribute;

            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; xmlRootAttribute.ElementName;
        }
    }&lt;/pre&gt;
&lt;p&gt;This solution worked just fine, and the code ran correctly, but I still wasn't happy with my solution. The problem I was sensing was that I was adding yet another extension method to Object, and Object's neighborhood was already pretty crowded with all the Linq methods in there. I wanted my extension methods to show up only on those classes to which I wanted to apply them. &lt;/p&gt;
&lt;p&gt;The solution that I came up with was to use a marker interface whose sole purpose is to limit the visibility of the extension methods to classes that I intend to apply them to. In this case, I made BaseRequest and BaseResponse each implement IMessageMarker, an interface with no methods. And I changed the extension method to be:&lt;/p&gt;&lt;pre&gt;    &lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; SssMessageExtensionMethods
    {
        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; GetCommandNameFromXmlRootAttribute(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; ISssMessageMarker message)
        {
            &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[] attributes = message.GetType().GetCustomAttributes(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(XmlRootAttribute), &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;);
            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (attributes.Length == 0) &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; message.GetType().Name;

            XmlRootAttribute xmlRootAttribute = attributes[0] &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; XmlRootAttribute;

            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; xmlRootAttribute.ElementName;
        }
    }&lt;/pre&gt;
&lt;p&gt;Now I have the same extension method defined, but it only appears on those classes that implement the marker. &lt;/p&gt;
&lt;p&gt;What do you think of this technique? In a more powerful language, like Ruby or C++ (ducking and running for cover!), this kind of trickery wouldn't be needed. But C# can only get you so far, so I felt this was a good tradeoff between adding the methods for needed functionality and making the most minimal change in my classes to hide these methods so that only those places that needed them could see them.&lt;/p&gt;
&lt;p&gt;-- bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23995.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>The downside of coding alone...</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2007/11/17/23810.aspx</link><pubDate>Sat, 17 Nov 2007 15:05:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2007/11/17/23810.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/23810.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2007/11/17/23810.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/23810.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/23810.aspx</trackback:ping><description>&lt;P&gt;I had what was probably an obvious insight the other day while I was working on my project alone. I'm a team of one, which kind of gets in the way when it comes to pairing. This, unfortunately, has an effect on my final code.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Good pairs are adversarial&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;When you find yourself pairing with someone really good, it can almost feel adversarial. What I mean by that is that you can get into a rhythm where one person writes a test, intending to lead his partner down the road of writing a particular piece of code. His partner, however, can write something entirely different that still causes the test to pass.&lt;/P&gt;
&lt;P&gt;This back and forth dance between tester and implementer forms the basis of good micro pairing sessions. In these sessions the tester/driver intends to lead the implementer down a particular path, but the implementer has the option of following another way, forcing the test writer to write another test, trying to drive the implementer down the intended path, and so on.&lt;/P&gt;
&lt;P&gt;This leads to particularly good code, as the code that is written is usually the least code possible to implement the functionality, and the tests that are written thoroughly cover the functionality that was intended. It's really cool to watch this work.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;If you're a pair of one...&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;If you happen to be working by yourself, it is very difficult to simulate this tension between test authoring and application implementation. At least, from my point of view, what happens is that I &lt;EM&gt;do&lt;/EM&gt; write the code I want to test to lead me to, regardless of whether or not there is a simpler way to get the test to pass. I think it is natural to do this, since you're trying to play both sides of the partnership.&lt;/P&gt;
&lt;P&gt;I think code I write without a pair is inferior to code I create with a partner, for this exact reason. We didn't fight over the minimal implementation, which leads to still good code, but not the glory that is fully paired/TDD code.&lt;/P&gt;
&lt;P&gt;There ain't nothing better.&lt;/P&gt;
&lt;P&gt;-- bab&lt;/P&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23810.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>Episode 2 - The InputReader and the start of the Processor</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2007/09/17/23374.aspx</link><pubDate>Mon, 17 Sep 2007 09:42:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2007/09/17/23374.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/23374.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2007/09/17/23374.aspx#Feedback</comments><slash:comments>12</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/23374.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/23374.aspx</trackback:ping><description>&lt;p&gt;OK, so this stuff is different. Really different. So different that i feel like a TDD rookie all over again. I find myself questioning everything that I do, and wondering if I&amp;#8217;m going in the right direction. But it&amp;#8217;s fun learning something new&amp;#8230;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When I last left you&amp;#8230;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we finished episode 1, we had created a couple of customer tests and had used Interaction Based Testing to drive out the basics of our architecture. In looking back at what we drove out, I  wonder about some of those classes. I can see that I have an input side, and processing middle, and an output side, but I see an awful lot of generalization having happened already. I&amp;#8217;m going to watch out for this throughout the rest of this exercise. It is possible that this style of writing tests drives you towards early generalization, but I&amp;#8217;m pretty sure it is just my unfamiliarity with how to drive code through these tests that is making this happen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Input Side&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;According to the first test I wrote, this is the interface that the input side needs to have:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public interface IInputReader
{
    List&amp;lt;BatchInput&amp;gt; ReadAllInputs();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I don&amp;#8217;t know anything about a &lt;em&gt;BatchInput&lt;/em&gt; yet, or how to read the input lines, but I think I may be about to find out.&lt;/p&gt;

&lt;p&gt;So my job now is to drive out how the &lt;em&gt;IInputReader&lt;/em&gt; will be implemented by some class. As it turns out, this is really not very interesting. The interaction-based testing that we&amp;#8217;ve been doing has been very useful at driving out interactions, but the &lt;em&gt;InputReader&lt;/em&gt; seems to stand alone. It is at the very end of the call chain, which means that it doesn&amp;#8217;t interact with anything else. This means that state-based tests will do just fine to test this piece of the system. &lt;/p&gt;

&lt;p&gt;Here is the first test I wrote for this class:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[Test]
public void InputReaderImplementsIInputReader()
{
    Assert.IsInstanceOfType(typeof(IInputReader), new InputReader(null));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I&amp;#8217;ve started writing tests like these to force me to make the class I&amp;#8217;m writing implement a specific interface. I started doing this because I&amp;#8217;ve found myself going along writing a class, knowing full well that it has to implement some interface, but forgetting to actually do it. I end up writing the whole class to the wrong API, and I have to go back and refactor the API to match what it should be. Hence, I write this test now to enforce me implementing the right interface.&lt;/p&gt;

&lt;p&gt;Here are the rest of the tests:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[Test]
public void EmptyInputStreamGeneratesEmptyOutputList()
{
    StringReader reader = new StringReader(String.Empty);
    InputReader inputReader = new InputReader(reader);

    List&amp;lt;BatchInput&amp;gt; input = inputReader.ReadAllInputs();

    Assert.AreEqual(0, input.Count);
}

[Test]
public void SingleCommandInputStringGeneratesSingleElementOutputList()
{
    StringReader reader = new StringReader("a|b" + System.Environment.NewLine);
    InputReader inputReader = new InputReader(reader);

    List&amp;lt;BatchInput&amp;gt; input = inputReader.ReadAllInputs();

    Assert.AreEqual(1, input.Count);
    Assert.AreEqual("a|b", input[0].ToString());
}

[Test]
public void MultipleCommandInputStringGeneratesMultipleElementsInOutputList()
{
    StringReader reader = new StringReader("a|b" + System.Environment.NewLine + "b|c" + Environment.NewLine);
    InputReader inputReader = new InputReader(reader);

    List&amp;lt;BatchInput&amp;gt; input = inputReader.ReadAllInputs();

    Assert.AreEqual(2, input.Count);
    Assert.AreEqual("a|b", input[0].ToString());
    Assert.AreEqual("b|c", input[1].ToString());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These tests follow the usual &lt;em&gt;0, 1, many&lt;/em&gt; pattern for implementing functionality. Make sure something works  for 0 elements, which fleshes out the API, then make sure it works for a single element, which puts the business logic in, and then make it work for multiple elements, which adds the looping logic. Here is the oh, so complicated code to implement these tests:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class InputReader : IInputReader
{
    private readonly TextReader reader;

    public InputReader(TextReader reader)
    {
        this.reader = reader;
    }

    public List&amp;lt;BatchInput&amp;gt; ReadAllInputs()
    {
        List&amp;lt;BatchInput&amp;gt; inputData = new List&amp;lt;BatchInput&amp;gt;();
        ReadAllLines().ForEach(delegate(string newLine) 
            { inputData.Add(new BatchInput(newLine)); });
        return inputData;
    }

    private List&amp;lt;string&amp;gt; ReadAllLines()
    {
        List&amp;lt;string&amp;gt; inputLines = new List&amp;lt;string&amp;gt;();
        while (reader.Peek() != -1)
        {
            inputLines.Add(reader.ReadLine());
        }

        return inputLines;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And that should pretty well handle the input side of this system. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On to the &lt;em&gt;Processor&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Processor&lt;/em&gt; class takes the input &lt;em&gt;BatchInput&lt;/em&gt; list and converts it into &lt;em&gt;ProcessOutput&lt;/em&gt; objects, which are then written to the output section of the program. Here is the interface again that rules this section of code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public interface IProcessor
{
    List&amp;lt;ProcessOutput&amp;gt; Process(List&amp;lt;BatchInput&amp;gt; inputs);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First of all, let&amp;#8217;s make sure that my class is going to implement the correct interface:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[Test]
public void ProcessorImplementsIProcessor()
{
    Assert.IsInstanceOfType(typeof(IProcessor), new Processor(null, null));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, the responsibilities that seem to have to happen here are that each &lt;em&gt;BatchInput&lt;/em&gt; object needs to be turned into something representing a payroll input line, and that new object needs to be executed in some way. Those thought processes lead me to this test:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[Test]
public void SingleBatchInputCausesStuffToHappenOnce()
{
    MockRepository mocks = new MockRepository();

    IPayrollProcessorFactory factory = mocks.CreateMock&amp;lt;IPayrollProcessorFactory&amp;gt;();
    IPayrollExecutor executor = mocks.CreateMock&amp;lt;IPayrollExecutor&amp;gt;();
    Processor processor = new Processor(factory, executor);
    PayrollCommand commonPayrollCommand = new PayrollCommand();

    List&amp;lt;BatchInput&amp;gt; batches = TestDataFactory.CreateBatchInput();

    using (mocks.Record())
    {
        Expect.Call(factory.Create(batches[0])).Return(commonPayrollCommand).Repeat.Once();
        executor.Execute(commonPayrollCommand);
        LastCall.Constraints(Is.Equal(commonPayrollCommand)).Repeat.Once();
    }

    using (mocks.Playback())
    {
        processor.Process(batches);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is tons of stuff to see in this test method now. Immediately after I create my &lt;em&gt;MockRepository&lt;/em&gt;, I create my system. This consists of three objects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;IPayrollProcessorFactory&lt;/em&gt;&lt;/strong&gt; &amp;#8212; responsible for converting the &lt;em&gt;BatchInput&lt;/em&gt; object into a &lt;em&gt;PayrollCommand&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;IPayrollExecutor&lt;/em&gt;&lt;/strong&gt; &amp;#8212; responsible for executing the &lt;em&gt;PayrollCommand&lt;/em&gt; after it is created&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Processor&lt;/em&gt;&lt;/strong&gt; &amp;#8212; the driver of the system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together, these three classes make up this portion of our system. If I were doing SBT (state-based testing), I&amp;#8217;m not entirely sure at all that I would have these two embedded objects yet. I would probably have written code and then refactored to get to where I am now. But, with IBT, you have to &lt;em&gt;think&lt;/em&gt; in terms of who the collaborators are that the method under test is going to use, and jump to having those collaborators now rather than later. In fact, the whole &lt;em&gt;IPayrollExecutor&lt;/em&gt; seems kind of contrived at this point to me, but I need to have something there to interact with, so I can write an IBT for this.&lt;/p&gt;

&lt;p&gt;On the next line, I create an instance of my &lt;em&gt;PayrollCommand&lt;/em&gt;. I specifically use this instance as the returned value from the &lt;em&gt;factory.Create&lt;/em&gt; call in the first expectation and as a constraint to the &lt;em&gt;executor.Execute&lt;/em&gt; in the second expectation. This was something I was struggling with earlier in my experimentation with IBT. What I want to have happen is that I want to force my code to take the object that is returned from the &lt;em&gt;Create&lt;/em&gt; call and pass it to the &lt;em&gt;Execute&lt;/em&gt; call. By having a common object that I use in the expectations, and having the &lt;em&gt;Is.Equal&lt;/em&gt; constraint in the second expectation, I can actually force that to happen. It took me a while to figure this out, and I&amp;#8217;m pretty sure that this is a Rhino Mocks thing, rather than a generic IBT thing, but I found this to be helpful.&lt;/p&gt;

&lt;p&gt;Then I drop into the record section, where I set some expectations on the objects I&amp;#8217;m collaborating with. The first expectation says that I expect an instance of a &lt;em&gt;BatchInput&lt;/em&gt; to be provided to this &lt;em&gt;Execute&lt;/em&gt; method when called. Please note, and it took me a while to intellectually really grasp this, the batches[0] that I&amp;#8217;m passing to the Create method is really just a place holder. This is the weird part here &amp;#8212; I&amp;#8217;m not actually calling the factory.Create method here, I&amp;#8217;m signaling the mocking framework that this is a method I&amp;#8217;m about to set some expectations on. I could have just as easily, in this case, passed in null in place of the argument, but I thought null didn&amp;#8217;t communicate very clearly. What I do mean is that I expect some instance of a &lt;em&gt;BatchInput&lt;/em&gt; to be provided to this method. Maybe I would have done better by new&amp;#8217;ing one up in place of using batches[0]???? It is not the value or identity of the object that matters here at all, it is the type, and only because a) the compiler needs it and b) it communicates test intent. The rest of that expectation states that I&amp;#8217;m only going to expect this method to be called once, and is allowing me to specify what object will be returned when this method is called. This last part is one of the hardest parts for me to have initially grasped. I was unsure whether this framework was asserting that the mocked method would return the value I passed it, or whether it was allowing me to set up the value that would be returned when it was called. In looking back, the second option is the only one that makes any sense at all, since these expectations are being set on methods that are 100% mocked out and have no ability to return anything without me specifying it in some way. Doh!&lt;/p&gt;

&lt;p&gt;The second expectation is where I set up the fact that I expect the same object that was returned from the Create call to be the object passed to this call. Again, I do this through the Constraint, not through the value actually passed to the executor.Execute() method. I could just as easily passed in null there, but it wouldn&amp;#8217;t have communicated as clearly. &lt;/p&gt;

&lt;p&gt;Finally, I get to the playback section, call my method, and the test is over.&lt;/p&gt;

&lt;p&gt;This is the code that I wrote to make this test pass:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public List&amp;lt;ProcessOutput&amp;gt; Process(List&amp;lt;BatchInput&amp;gt; batches)
{
    List&amp;lt;ProcessOutput&amp;gt; results = new List&amp;lt;ProcessOutput&amp;gt;();

    PayrollCommand command = factory.Create(batches[0]);
    executor.Execute(command);

    return results;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I know I&amp;#8217;m not handling the results at all yet, but I&amp;#8217;m pretty sure I can flesh out what will happen with those at some point soon.&lt;/p&gt;

&lt;p&gt;In my second test, I&amp;#8217;ll worry about how to handle multiple &lt;em&gt;BatchInput&lt;/em&gt; objects. Again, this is a very common pattern for me, starting with one of something to get the logic right, and then moving on to multiple, to put in any looping logic I need. Here is the second test:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[Test]
public void MultipleBatchInputsCausesStuffToHappenMultipleTimes()
{
    MockRepository mocks = new MockRepository();

    IPayrollProcessorFactory factory = mocks.CreateMock&amp;lt;IPayrollProcessorFactory&amp;gt;();
    IPayrollExecutor executor = mocks.CreateMock&amp;lt;IPayrollExecutor&amp;gt;();
    Processor processor = new Processor(factory, executor);
    PayrollCommand commonPayrollCommand = new PayrollCommand();

    List&amp;lt;BatchInput&amp;gt; batches = TestDataFactory.CreateMultipleBatches(2);

    using (mocks.Record())
    {
        Expect.Call(factory.Create(batches[0])).
                Constraints(List.OneOf(batches)).Return(commonPayrollCommand).Repeat.Twice();
        executor.Execute(commonPayrollCommand);
        LastCall.Constraints(Is.Equal(commonPayrollCommand)).Repeat.Twice();
    }

    using (mocks.Playback())
    {
        processor.Process(batches);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Almost all of this test is exactly the same, except I add two &lt;em&gt;BatchInput&lt;/em&gt; objects to my list. The only other thing I need to enforce is that the object that is passed to the &lt;em&gt;factory.Create&lt;/em&gt; method is a &lt;em&gt;BatchInput&lt;/em&gt; object that is a member of the list I passed in, which I do with the List Constraint to the first expectation.  &lt;/p&gt;

&lt;p&gt;Here is the modified &lt;em&gt;Processor&lt;/em&gt; code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public List&amp;lt;ProcessOutput&amp;gt; Process(List&amp;lt;BatchInput&amp;gt; batches)
{
    List&amp;lt;ProcessOutput&amp;gt; results = new List&amp;lt;ProcessOutput&amp;gt;();

    foreach (BatchInput batch in batches)
    {
        PayrollCommand command = factory.Create(batch);
        executor.Execute(command);
    }

    return results;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;Object Mother&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In both of these tests, you&amp;#8217;ll see a reference to &lt;em&gt;TestDataFactory&lt;/em&gt;. This is a class whose responsibility it is to create test data for me when asked. I use it to remove irrelevant details about test data from my tests and move it someplace else. This is called the Object Mother pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In the next episode&amp;#8230;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s about enough for now. If any of this wasn&amp;#8217;t clear, please let me know, and I&amp;#8217;ll update the text to be better. In the next episode, I&amp;#8217;ll go ahead and build the factory using SBT, since it isn&amp;#8217;t going to interact with anything and then dive into the &lt;em&gt;Processor&lt;/em&gt; code, which should prove interesting.&lt;/p&gt;

&lt;p&gt;Overall, I&amp;#8217;m pretty happy with how IBT is allowing me to focus on interactions between objects and ignore details like the contents of my domain classes entirely until I get to a class who manipulates the contents of those domain classes. &lt;/p&gt;

&lt;p&gt;My biggest question lies in the area of premature generalization. Am I thinking too much and ignoring YAGNI? Do these tests reflect the simplest thing that can possibly work? I&amp;#8217;m truly not sure. I tried to do better in this episode to focus on just payroll stuff and not make generic classes, like the &lt;em&gt;IInputReader&lt;/em&gt;. I have a &lt;em&gt;PayrollProcessorFactory&lt;/em&gt;, for example, instead of a &lt;em&gt;ProcessorFactory&lt;/em&gt;. Those refactorings will come, and I want to wait for the code to tell me about them. IBT, I think, makes it easier to see those abstractions ahead of time, but I need to resist!&lt;/p&gt;

&lt;p&gt;Please write with questions and comments. This continues to be an interesting journey for me, and I&amp;#8217;m not at all sure where I&amp;#8217;m going yet! But it is fun!&lt;/p&gt;

&lt;p&gt;&amp;#8212; bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23374.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>Interesting difference using nested test suites in JUnit versus NUnit</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2007/09/10/23341.aspx</link><pubDate>Mon, 10 Sep 2007 10:58:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2007/09/10/23341.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/23341.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2007/09/10/23341.aspx#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/23341.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/23341.aspx</trackback:ping><description>&lt;p&gt;My friend, &lt;a href="http://blogs.msdn.com/jamesnewkirk/"&gt;Jim Newkirk&lt;/a&gt;, introduced me to a very nice way of partitioning programmer tests for a class as you write them. Most developers write a single test class for a single application class, and just dump all tests for that class in the same place. This is not as correct as it could be (that&amp;#8217;s Consultant-Speak for &amp;#8220;that&amp;#8217;s just plain wrong&amp;#8221;). &lt;/p&gt;

&lt;p&gt;The accepted best practice is to group together tests that have the same setup/teardown logic into the same test fixtures, which can lead to having multiple fixtures for a single class. For example, when I build a Stack class, I generally have different fixtures for each of the different states that my Stack class can have, and I put a test into the correct fixture representing its starting state. For example, I  might have states corresponding to &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an empty stack&lt;/li&gt;
&lt;li&gt;stack with a single element&lt;/li&gt;
&lt;li&gt;stack with multiple elements&lt;/li&gt;
&lt;li&gt;stack that is full&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and so on. I would create a new fixture for each of these states, and use setup and teardown to push my system into the given state for that fixture. I know that this is a departure from my previous advice about &lt;a href="http://www.agileprogrammer.com/oneagilecoder/archive/2005/07/23/6261.aspx"&gt;Assiduously Avoid Setup and Teardown&lt;/a&gt;, but I think I like where this leads me. I promise to post an example of writing tests like this over the next few days, but that example is not part of what I&amp;#8217;m talking about here.&lt;/p&gt;

&lt;p&gt;What I am talking about is an arrangement like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[TestFixture]
public class StackFixture
{
    [TestFixture]
    public class EmptyFixture
    {
        [Test]
        public void ATest() {}
    }

    [TestFixture]
    public class SingleElementFixture
    {
        [Test]
        public void AnotherTest() {}
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and so on. The main reason I like this arrangement of using nested fixtures is that it allows for me to separate out tests for different behaviors of my class into different fixtures, which lets me find them more easily and makes it easier to decide where to put new tests, and it lets me run all the tests for a particular class together at the same time. If I were to have several independent test fixtures, I would have no automated way of ensuring that I ran all of them together. The closest I could come would be to use categories, which is rather manual and error prone.&lt;/p&gt;

&lt;p&gt;Now, what I just tried to do was to replicate this arrangement in Java, and it was harder to make it work. Using JUnit 3.8.1, I tried this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class StackFixture extends TestCase {
    public static class EmptyFixture extends TestCase {
        public void testATest() {}
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This gave me two tests run, one failing, so it found the test in the inner class, but found an error about no test being found in the outer fixture, StackFixture, so my tests could never all pass. I tried removing static from the class declaration, and then JUnit didn&amp;#8217;t find the test in the inner fixture, and I still had the failure for no tests found. Clearly, not possible here.&lt;/p&gt;

&lt;p&gt;Then I tried JUnit 4, which, like NUnit 2 and beyond, uses attributes to identify tests. In Java, they call them annotations, but they seem to be the same thing. Here is what I wrote:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class JUnitFourFixture {
    public  class StackEmptyFixture {
        @Test
        public void EmptyAtCreation() {
            Stack stack = new Stack();

            assertTrue(stack.isEmpty());
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When I ran this, I got an error popup saying that there were no tests found. Not a winner :( But when I added static to the class declaration for the inner class, things did finally work beautifully. Here is the code that worked, with an extra fixture added just to be sure, and the inner classes made static. (BTW, for those of you who don&amp;#8217;t know, there are two kinds of inner classes in Java. Inner classes without the static prefix belong to a particular instance of the enclosing class, so when you instantiate the outer class, you&amp;#8217;re instantiating the inner class as well, and it has access to stuff in the outer object. If you have the static prefix, then the inner class is entirely independent of the outer class, just like inner classes in C#.)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class JUnitFourFixture {
    public static class StackEmptyFixture {
        @Test
        public void EmptyAtCreation() {
            Stack stack = new Stack();

            assertTrue(stack.isEmpty());
        }
    }

    public static class SingleElementFixture {
        @Test
        public void AnotherTest() {
            assertTrue(true);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I don&amp;#8217;t know how many of you didn&amp;#8217;t know this, or never had a reason to care about this, but I am teaching a Java TDD course this week. Before I taught this, I wanted to make sure it worked!&lt;/p&gt;

&lt;p&gt;&amp;#8212; bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23341.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>More information about Interaction Based Testing and mock object frameworks</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/30/23250.aspx</link><pubDate>Thu, 30 Aug 2007 13:11:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/30/23250.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/23250.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/30/23250.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/23250.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/23250.aspx</trackback:ping><description>&lt;p&gt;Craig Buchek pointed me to a great paper called, &lt;em&gt;&lt;a href="http://mockobjects.com/files/mockrolesnotobjects.pdf"&gt;Mock Roles, Not Objects&lt;/a&gt;&lt;/em&gt;. I thought it was a good explanation of what I was trying to show in my last post. &lt;/p&gt;
&lt;p&gt;Recommended reading.&lt;/p&gt;
&lt;p&gt;-bab&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23250.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>Deep Dive into TDD Revisited</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/29/23226.aspx</link><pubDate>Wed, 29 Aug 2007 15:03:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/29/23226.aspx</guid><wfw:comment>http://www.agileprogrammer.com/oneagilecoder/comments/23226.aspx</wfw:comment><comments>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/29/23226.aspx#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.agileprogrammer.com/oneagilecoder/comments/commentRss/23226.aspx</wfw:commentRss><trackback:ping>http://www.agileprogrammer.com/oneagilecoder/services/trackbacks/23226.aspx</trackback:ping><description>&lt;p&gt;Hi, everyone. I haven&amp;#8217;t posted any serious technical content on this blog for a long time now. The reason for this is that I&amp;#8217;m now a pointy haired boss most of the time. I spend my days teaching, mentoring, coaching, and occasionally pairing with someone on another team. I miss coding&amp;#8230; I really do. &lt;sniff&gt;&lt;/p&gt;

&lt;p&gt;However, I&amp;#8217;ve been digging into &lt;em&gt;Interaction Based Testing&lt;/em&gt; over the past few weeks, and I&amp;#8217;ve found it fascinating. The road I took to get here involved trying to learn more about what &lt;a href="http://behaviour-driven.org"&gt;Behavior Driven Development&lt;/a&gt; is, and why so many people I know and respect seem to like it, or at least appreciate it. One of the techniques that BDD uses is something called Interaction Based Testing, or IBT for short. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interaction Based Testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;IBT is different from traditional TDD in that it is defining and verifying the interactions between objects as they take place, rather than defining and verifying that some input state is being successfully translated to some output state. This latter kind of testing, called &lt;em&gt;State Based Testing&lt;/em&gt;, or SBT for short, is what I had always done when I did TDD (for the most part). IBT involves using a Mock Object Framework that allows you to set expectations on objects that your class under test is going to call, and then helps you verify that each of those calls took place. Here is a short example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[TestFixture]
public class IBTExample
{
    [Test]
    public void SampleITBTest()
    {
        MockRepository mocks = new MockRepository();

        IListener listener = mocks.CreateMock&amp;lt;IListener&amp;gt;();
        Repeater repeater = new Repeater(listener);

        listener.Hear("");
        LastCall.On(listener).Constraints(Is.NotNull()).Repeat.Once();

        mocks.ReplayAll();

        repeater.Repeat("");

        mocks.VerifyAll();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The basic problem that I&amp;#8217;m trying to solve here is that I can write a method, Repeat(), on a class called Repeater such that when I call Repeat(), it repeats what it was passed to its IListener. The way that I set this up is more complicated than I would use in a state-based test, but I avoid cluttering my test with irrelevant implementation details (like explicit data).&lt;/p&gt;

&lt;p&gt;What this test is doing is creating the system and setting expectations on the IListener that define how the Repeater class is going to use it at the appropriate time. The MockRepository is the class that represents the mock object framework I&amp;#8217;m using, which in this case is &lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;Rhino Mocks&lt;/a&gt;. I new one of these up, and it handles all the mocking and verification activities that this test requires. On the next line, you see me creating a mock object to represent an IListener. I typically would have created a state-based stub for this listener that would simply remember what it was told, for my test to interrogate later. In this case, the framework is creating a testing version of this interface for me, so I don&amp;#8217;t have to build my own stub. Next, I create the class under test and wire it together with the listener. Nothing fancy there. &lt;/p&gt;

&lt;p&gt;The next line looks a little strange, and it is. It is actually a result of how this particular mocking framework functions, but it is easily understood. While it may look like I&amp;#8217;m calling my listener&amp;#8217;s Hear method, I&amp;#8217;m actually not. When you create an instance of the mocking framework, it is created in a recording mode. What this means is that every time you invoke a method on a mocked out object while recording, you are actually calling a proxy for that object and defining expectations for how that object will be called in your regular code later. In this case (admittedly, not the simplest case), listener.Hear() is a void method, so I have to split the setting of expectations into two lines. On the first line, I call the proxy, and the framework makes a mental note that I called it. On the next line, I say to the framework, &amp;#8220;Hey, remember that method I just called? Well, in my real code, when I call it, I expect that I am going to pass it some kind of string that will never be null, and I&amp;#8217;ll call that method exactly once. If I do these things, please allow my test to pass. If I don&amp;#8217;t do them, then fail it miserably&amp;#8221;.&lt;/p&gt;

&lt;p&gt;After I set up the single expectation I have on the code I&amp;#8217;m going to be calling, I exit record mode and enter replay mode. In this mode. the framework allows me to run my real code and plays back my expectations for me while my real code executes. The framework keeps track of whatever is going on, and when I finally call my application method, Repeater.Repeat() in this case, followed by the mocks.VerifyAll(), it checks to make sure that all expectations were met. If they were, I&amp;#8217;m cool, otherwise my test fails.&lt;/p&gt;

&lt;p&gt;I hope that was at least a little clear. It was very confusing to me, but I sat down with a few folks at the agile conference two weeks ago, and they showed me how this worked. I&amp;#8217;m still very new at it, so I&amp;#8217;m likely to do things that programmers experienced with this kind of testing would find silly. If any of you see something I&amp;#8217;m doing that doesn&amp;#8217;t make sense, please tell me!&lt;/p&gt;

&lt;p&gt;Here is the code this test is forcing me to write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class Repeater
{
    private readonly IListener listener;

    public Repeater(IListener listener)
    {
        this.listener = listener;
    }

    public void Repeat(string whatToRepeat)
    {
        listener.Hear(whatToRepeat);
    }
}

public interface IListener
{
    void Hear(string whatToHear);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Advantages to IBT style TDD&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are several things about this that I really like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It allows me to write tests that completely and totally ignore what the data is that is being passed around. In most state-based tests, the actual data is irrelevant. You are forced to provide some values just so that you can see if your code worked. The values obfuscate what is happening. IBT allows me to avoid putting any data into my tests that isn&amp;#8217;t completely relevant to that test, which allows me to focus better on what the test is saying.&lt;/li&gt;
&lt;li&gt;It allows me to defer making decisions until much later. You can&amp;#8217;t see it in this example, but I&amp;#8217;m finding that I&amp;#8217;m much better able to defer making choices about things until truly need to make them. You&amp;#8217;ll see examples of this in the blog entries that are to follow (more about this below).&lt;/li&gt;
&lt;li&gt;I get to much simpler code than state-based testing would lead me to&lt;/li&gt;
&lt;li&gt;My workflow changes. I used to
&lt;ol&gt;
&lt;li&gt;Write a test&lt;/li&gt;
&lt;li&gt;Implement it in simple, procedural terms&lt;/li&gt;
&lt;li&gt;Refactor the hell out of it&lt;/li&gt;
&lt;/ol&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With ITB, I&amp;#8217;m finding that it is really hard to write expectations on procedural code, so my code much more naturally tends to lots of really small, simple objects that collaborate together nicely. I am finding that I do refactoring less frequently, and it is usually when I&amp;#8217;ve changed my mind about something rather than as part of my normal workflow. This is new and interesting to me.&lt;/p&gt;

&lt;p&gt;There are some warts that I&amp;#8217;m seeing with it, and I&amp;#8217;ll get to those as well, as I write further in this series. I&amp;#8217;m also very certain that this technique has its time and place. One of the things I want to learn is where that time and place is. Anyhow, here are my plans for this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Revisiting my Deep Dive&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I want to redo the example I did a couple years ago when I solved the &lt;a href="http://www.agileprogrammer.com/oneagilecoder/archive/2004/10/25/2805.aspx"&gt;Payroll&lt;/a&gt; &lt;a href="http://www.agileprogrammer.com/oneagilecoder/archive/2004/11/07/2799.aspx"&gt;problem&lt;/a&gt; &lt;a href="http://www.agileprogrammer.com/oneagilecoder/archive/2004/11/15/2793.aspx"&gt;in a&lt;/a&gt; &lt;a href="http://www.agileprogrammer.com/oneagilecoder/archive/2004/11/25/2771.aspx"&gt;6-part&lt;/a&gt; &lt;a href="http://www.agileprogrammer.com/oneagilecoder/archive/2004/12/02/2767.aspx"&gt;blog&lt;/a&gt; &lt;a href="http://www.agileprogrammer.com/oneagilecoder/archive/2004/12/04/2756.aspx"&gt;series&lt;/a&gt;. I want to solve the same problem in a ITB way, and let you see where it leads me. I&amp;#8217;ve done this once already, part of the way, just to learn how this worked, and the solution I came up with was very different than the one I did the first time. I&amp;#8217;m going to do this new series the exact same way as the old series, talking through what I&amp;#8217;m doing and what I&amp;#8217;m thinking the whole time. I&amp;#8217;m personally very curious to see where it goes.&lt;/p&gt;

&lt;p&gt;Once we&amp;#8217;re finished, I want to explore some other stories that are going to force me to refactor some of my basic design assumptions, because one of the knocks against ITB is that it makes refactoring harder by defining the interactions inside your tests &lt;em&gt;and&lt;/em&gt; your code. We&amp;#8217;ll find out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please ask questions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m learning this stuff as I go, so I&amp;#8217;m very eager to hear criticisms of what I&amp;#8217;ve done and answer questions about why I&amp;#8217;ve done things. Please feel free to post comments on the blog about this and the following entries. I&amp;#8217;m really looking forward to this, and I hope you readers are, too.&lt;/p&gt;

&lt;p&gt;&amp;#8212; bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23226.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>