Refactoring Unit Tests Pays Off

Sometimes refactoring UnitTests is a great way to explore, understand and even solve a problem.

I recently needed to model a triangular grid of triangles, each of which knew the identity of its neighbors. Some have one neighbor, some have two and others have three. I had no idea how the math should work, so I set out to write a brute force test of a grid 3 rows long (RuleOfThree). About half way into the third row a pattern emerged. I quickly refactored the test to cover every neighbor of every triangle in any triangular grid. To satisfy the unit test, I copied the test code to the production class and changed the 7 "assertEquals" statements into assignments.


EditText of this page (last edited June 14, 2003) or FindPage with title or text search