<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>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><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><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><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><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><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><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><item><dc:creator>Brian Button</dc:creator><title>Agile 2007</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/10/23132.aspx</link><pubDate>Fri, 10 Aug 2007 11:29:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/10/23132.aspx</guid><description>&lt;p&gt;I'm eagerly looking forward to going to Agile 2007 next week. I look forward to this conference every year to give me energy to get through the next year. It's so much fun to see all my agile friends for a week, hang out, talk about what we've learned, drink the occasional beer, and just catch up.&lt;/p&gt;


&lt;p&gt;If you're going to DC this year, look me up. I'm in the conference hotel.&lt;/p&gt;


&lt;p&gt;I'd also like to suggest a BDD BoF session, if anyone going is interested in that. I'll try to sign up for it some night there.&lt;/p&gt;


&lt;p&gt;Finally, I'm giving 2 talks this year. The first talk is an Experience Report on Tuesday morning. It's about building trust with a customer as a key success factor. On Friday morning, Peter Provost and I are giving a talk called "Agile is more than 'Monkey See, Monkey Do'". The point of this talk is to point out to those who might be new to agile that there is more to it than just the practices, it is possible to be agile without doing ANY of the practices, and that doing the practices doesn't necessarily make you agile.&lt;/p&gt;


&lt;p&gt;See you all there!&lt;/p&gt;

&lt;p&gt;-- bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23132.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>How to create a maintainable system</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/10/23131.aspx</link><pubDate>Fri, 10 Aug 2007 08:51:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2007/08/10/23131.aspx</guid><description>&lt;p&gt;&lt;a href="http://ayende.com/"&gt;Ayende&lt;/a&gt; had an interesting post on his blog today about &lt;a href="http://feeds.feedburner.com/~r/AyendeRahien/~3/142698733/The-only-metric-that-counts-Maintainability.aspx"&gt;the only metric that really counts&lt;/a&gt;, which is maintainability. He made a joke about measuring this property of a system by measuring the intensity of groans that emanate from the team when asked to changed something, which made me laugh.&lt;/p&gt;

&lt;p&gt;It did bring up a more significant question in my mind, one that I've thought about before, and something that I've been telling my TDD classes lately. The question always comes up in class about why we just don't design our systems right in the first place, for some value of right. There are several parts to my answer, but one of them is always that we build our systems simply and change them as we need to so that we can practice changing our systems for our big moment. That big moment happens when the customer comes in and tells us that he really, really, really needs this new feature, and he needs it by next Thursday.&lt;/p&gt;

&lt;p&gt;If we had never practiced changing our system, and had never considered changing our system, then this might be a pretty scary thing. But if you build your system such that you practice changing it in new and interesting ways from the first day, when that change request comes in, you yawn and make the change.&lt;/p&gt;

&lt;p&gt;In short, you get a maintainable system by practicing maintenance from the first day.&lt;/p&gt;

&lt;p&gt;-- bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23131.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>Interesting lesson learned while teaching TDD this week...</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2007/07/12/23034.aspx</link><pubDate>Thu, 12 Jul 2007 16:22:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2007/07/12/23034.aspx</guid><description>&lt;p&gt;I've been preaching TDD for years, and one of the lessons that I've taught over and over is that if a test is hard to write, you have probably bitten off more than you can chew. Comment out this test, try a smaller byte, and come back to this test later. &lt;/p&gt; &lt;p&gt;I need to listen to myself more often :)&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Background&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;It has always been difficult for me to explain to students how TDD works with multiple objects, refactoring, stubs, and the whole shebang. They get the refactoring part, they get the writing a test and them some code part, but their understanding falls apart when I start talking about testing void methods. This is where test doubles of some sort come in, and I swear, I've seen more blank stares when I explain this than I have any other time in the rest of my 42 years.&lt;/p&gt; &lt;p&gt;So I did something about it this class.&lt;/p&gt; &lt;p&gt;I took my own advice.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The change&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Instead of having a big jump from TDD on a single class to this whole-hog example, I put in a couple of smaller examples, with exercises,&amp;nbsp;between the two existing endpoints. In the first one, I introduce a test double that allows them to test that the side-effect of calling a method happens correctly. It takes a single interface and a single stub to implement this new concept, and they get it! Then I introduce another stub that provides data into the class under test, which requires another interface and stub class. And they still get it! Now we're at the point where we'd be previously, but I don't see any blank stares. Victory!&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The lesson&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;em&gt;If a test is hard to write, then you've probably bitten off more than you can chew. Comment it out and try a smaller byte.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;That advice works for the instructor, too!&lt;/p&gt; &lt;p&gt;-- bab&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23034.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Brian Button</dc:creator><title>If something is hard, do it more often, until it becomes easy!</title><link>http://www.agileprogrammer.com/oneagilecoder/archive/2007/07/06/23005.aspx</link><pubDate>Fri, 06 Jul 2007 09:32:00 GMT</pubDate><guid>http://www.agileprogrammer.com/oneagilecoder/archive/2007/07/06/23005.aspx</guid><description>A team I was managing recently shipped a pretty complex, interactive web site, &lt;a href="http://www.sothebyshomes.com"&gt;http://www.sothebyshomes.com&lt;/a&gt; . Feel free to check it out and let us know how you feel. During this project, there were a bunch of things that were really hard for us to do, and we invented ways of making them easier. By making them easier, we did them more frequently, and their value to the project and customer increased.&lt;/p&gt;

&lt;b&gt;Deployment&lt;/b&gt;
&lt;p&gt;The particular aspect of our system I want to talk about is our deployment process. I'll go ahead and credit &lt;a href="http://www.adamesterline.com"&gt;Adam Esterline&lt;/a&gt; with coming up with our scheme, but I'm pretty sure that the rest of the team had significant input into it.&lt;/p&gt;

&lt;i&gt;Solving the local issue&lt;/i&gt;
&lt;p&gt;So when we started this project, we all shared a single SQLServer instance, running on our build server. It was filled with a bunch of canned data against which our tests ran and we manually played around with our system. Over time, as we had to add more and more data to our system to make individual tests work, and had to alter the schema to add functionality, it became harder to share a single instance of the database. Adam at this point adopted the Rails idea of Migrations, and implemented it for us in .Net. This allowed us to do a checkout and build locally, which ended up creating a local database for us on each pairing station, where the database was fully up-to-date with everything that had been done so far. The way it was set up, the mere act of checking out our code and running a build gave us a fully working system, which is just what you want.&lt;/p&gt;

&lt;i&gt;Solving the staging issue&lt;/i&gt;
&lt;p&gt;As the project progressed further, we wanted to do deployments to our staging server located somewhere on the East Coast of the US. So we started off doing this deployment manually for a month or so, and it was getting to be a major pain. There was a wiki page on our local server that outlined the multi-step process, including how to log into the VPN, log into the staging server, how and where to copy our files, how to deploy them once they were there, and so on. We had a multi-gigabyte database image that needed to be uploaded over a very shaky VPN connection, which could take several days to do successfully, so we only did it when absolutely necessary. It was ugly. This process took like an hour to do manually (aside from the DB copy, which took 6 hours on a good day), and we were doing it multiple times a week. It was really painful, but also really necessary. So we adapted our build system to allow us to log into the staging server and run our standard build process. Like above, this gave us the ability to just do a checkout and kick off a build, and everything was updated to where it should be, courtesy of the migrations we had. So this problem was solved. Deployment to our staging server was something that we could trivially do. Yea!&lt;/p&gt;

&lt;i&gt;Solving the production issue&lt;/i&gt;
&lt;p&gt;As we got closer to the end, we had to deploy our system to yet another set of servers. This time, we needed to put our system onto the production servers so that we could do our final testing before release. This situation was different, as the production servers were a pair of load balanced machines with a single backend SQLServer box. We figured that we would be deploying many times over the lifetime of our project, so it made sense to make the deployment process as easy as possible. So, we just adapted our existing deployment process to add the new target of the production servers, which allowed us to log into either of the web servers, do a build, and have a working system with a single command. Then we could log into the other web server, run another build, and have both servers updated trivially.&lt;/p&gt;

&lt;p&gt;Before we could get to this point, however, we had to spend about 3 days getting a version of our database up there, manually tweak the deployment process a few times to get around environmental issues that existed on the deployment web and database servers that didn't exist in staging (permissions mostly), and practice doing it. But once we got to the Thursday night that we were asked to go live, we did it. About 4 times :) And its been trivial ever since.&lt;/p&gt;

&lt;b&gt;The lesson&lt;/b&gt;
&lt;p&gt;Deploying our system to staging and production were important activities for us to do. We had to do them often, and we had to do them reliably. The process was completely identical from deploy to deploy, so it seemed to be an ideal candidate for automation. So, after doing this process by hand a bazillion times, we finally automated it, and it got better. Instantly. We found something that was very hard for us to do, did it enough times to understand the problem, and automated the problem away.&lt;/p&gt;

&lt;p&gt;The curious part is that this scenario is not unique to our project at all. So many of my clients have completely manual deployment processes, spanning several departments and management ladders, and their deployments often fail. When they fail, developers and operations people take hours or days to figure out what went wrong this time. In many of these cases, these people could automate away their problems. It would take cooperation across groups, cooperation across departments, it would take some people letting go of their fiefdoms of deployment. But it would help the overall company succeed and become more, dare I say it, agile.&lt;/p&gt;

&lt;p&gt;
-- bab
&lt;/p&gt;&lt;img src ="http://www.agileprogrammer.com/oneagilecoder/aggbug/23005.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>