Writing Unit Test for Legacy Code

Sandro Mancuso recently published a video on “Testing and Refactoring Legacy Code.” One of the key benefits for writing test for legacy code is it allows you to understand the existing code while reducing risk in changing it.

It is a great video but a bit long so here are some of his suggestions.

  • Do not change production code unless it is covered by a test.
  • Start testing from shortest to deepest branch. The Shortest branch will require less understanding of the code base.
  • Start refactoring from deepest to shortest branch. 
  • Do not execute another class from the unit test. The test should only care about the class it is testing. Otherwise you may be testing components (database, external systems, etc…) that you don’t want to test right now. Refactor the Singleton, Static call, or object to a protected method (Seam) and overwrite it.
  • Feature envy is when one class/method has functionality that probably belongs in another class.
  • Guard clause is a parameter check and should be moved earlier.
  • Bring declaration of variables and the code that uses the variable closer together.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s