|
Preserve Your Legacy
Enable unit testing of legacy code through refactoring patterns
by Russell Gold
April 14, 2005
Java developers are rapidly discovering the advantages of refactoring to fix architectural problems in existing code. Refactoring properly requires fast unit tests that can verify the refactoring doesn't break existing functionality. However, many of us work on systems that do not already have unit tests, and in many cases those systems do not easily lend themselves to unit testing as they stand.
This situation leads to an apparent deadlock in which refactoring cannot be done without tests, but tests cannot be written until refactoring is done. Therefore, let's look at a pattern-based approach to supporting legacy code, retrofitting unit tests, and enabling refactoring.
The first step is to recognize that you really can change your code safely, even without tests. Don't be surprised. Most developers have been doing exactly that for years, relying on occasional manual tests, long development cycles, and external testing to catch the worst problems. Refactoring makes this process much safer in that it involves well-defined steps, each of which is virtually guaranteed to improve the structure of the code while maintaining its behavior. Many of these steps can be automated. IntelliJ IDEA, Eclipse, and the latest version of Oracle JDeveloper are among the development environments that support several common refactorings, allowing for very safe code changes.
If you must refactor before adding tests, you want to do as little as possible, focusing on the specific goal of making future changes safe and easy. Taking time to consider what makes systems easy to change is well worth the effort.
Back to top
|