Saturday, March 26, 2011

Can you instantiate your application-under-test in one line?

Can you instantiate your application-under-test in one line? As in your entire application, with all externally facing interactions (threads, GUI, databases, network, etc.) faked out?

Like this:

[TestFixture]
public class MyApplicationFunctionalTests
{
    [Test]
    public void ShouldValidateAdditionalNewFeature()
    {
        var sut = new MyApplication();
        sut.DataArrivesOnNetwork("message from another user");
        sut.AssertGuiDisplays("message from another user");
    }
}

The ability to instantiate your application-under-test in one line enables you to immediately and easily write a test for any new feature or bug fix, and see the effects on the entire application. The power of this technique is difficult to overstate. The true benefits of TDD are much more evident using functional testing with real classes at the application level than at the unit test level using a mocking framework.

I picked up this technique from Growing Object-Oriented Software, Guided By Tests. While the authors and I disagree about the effectiveness of mocking frameworks (they are the authors of JMock2), we certainly agree on the use of functional testing at the application level (we also agree on DI containers being detrimental). I tried the AUT-in-one-line technique with a recent greenfield-ish project and the results were very positive. All projects I start going forward will definitely use this pattern.

0 comments:

Post a Comment