A roadmap for unit testing

December 04, 2012

I was recently asked by an old friend how they can get started writing unit tests on their existing application.  He was looking for guidance and more or less a roadmap of how to get started.  I decided to share what I sent him here.

So, if there is already code written for an application that you want to add unit tests to, this is harder, but not impossible.  Essentially, the problem you will run into is the code you are trying to unit test isn't testable.

Working Effectively with Legacy Code
Production code without unit tests is considered legacy code.  More than likely it will no small task adding unit tests to your existing application.  Most legacy applications were not designed to be testable.  The code has concrete dependencies that make it hard externalize during unit testing to isolate the class.

I would consider reading up on how to unit test legacy code.  The seminal tome on this topic is Michael Feather's "Working Effectively with Legacy Code".  The title doesn't sound sexy, but it is essentially a guide for how to write unit tests for a legacy application.  If you don't have time for the book, you can get a preview of his thoughts and techniques in this presentation of his and in this prebook article of his thoughts.

You can follow these steps to add unit tests to your code.  You may progress through them over the course of days or weeks.  I make assumptions that you are working on a Java application, but most of the guidance can be applied generally.

  1. Setting up the code
    1. Add JUnit, Mockito and PowerMock (optional) to your project's build script tool
      1. Use dependency management like Maven, Gradle, or Ivy.
      2. If you aren't using a build tool, stop and add one now!
    2. Run the build tool's eclipse setup (mvn eclipse:eclipse or gradle cleanEclipse eclipse)
      1. I prefer using the build tool to manage IDE project files.
    3. Open Eclipse/IntelliJ and you should be able to create unit tests
  2. Learning to write good unit tests
    1. Learn JUnit syntax
    2. A unit test should fit into this criteria.
    3. Write each test in the Arrange Act Assert format
      1. FYI, this is the same as Given When Then, which is Behavior Driven Development.
    4. Use this guide as well to figure out which code to start testing:
      1. I wouldn't move on to the next step until you feel comfortable
  3. Using Test Doubles
    1. First, learn what Test Doubles are.
    2. Mock Roles, Not Objects
    3. Mockito has some great documentation.
    4. The best place to learn how to use Mock object properly is by reading this book: Growing Object Oriented Code, Guided by Tests
  4. Learning TDD
    1. The best way to learn, is to practice.
    2. Learn from Uncle Bob's Bowling Game Kata
      1. Download his Powerpoint and follow along.  Pretty good.
      2. Try to solve this particular problem on your own.  Just write code and throw it away.  The experience is important
    3. DC Software Craftsmanship User Group - all they do is do TDD.  It's awesome!
    4. Try these other code katas

The process of learning to write good unit tests, how use Test Doubles, how to do TDD and finally how it all fits together can a long time to set in, but it is well worth it.

Share this:

comments powered by Disqus