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