I’m working with legacy java code, without any Unit-Tests. Many classes need to be refactored in order to work with the project.
Many refactorings can be done with eclipse, and am I doing some by hand. After some refactoring I review the diff to cvs-HEAD, but i can’t really feel certain that everything is 100% correct.
The Question: How can I validate a refactoring, that is mathematical identical to the previous version? I wish there were a tool, but i also accept “basic human algorithms” as solutions.
I know, “run your JUnit-Tests” is the best answer, but sadly, there aren’t any in my project.
Thank you!
In “TDD By Example” there is a specific section that talks about it. The problem is that you need unit tests to refactor, but a complicated code is usually non-testable. Thus, you want to refactor to make it testable. Cycle.
Therefore the best strategy is as follows:
Do tiny refactoring steps. When the steps are small it is easier for a human to make sure the overall behavior is intact. Choose only refactorings that increase testability. This is your immediate goal. Don’t think about supporting future functionality (or anything fancy like that). Just think about “how can I make it possible for a unit test to test this method/class”.
As soon as a method/class becomes testable, write unit tests for it.
Repeating this process will gradually get you to a position where you have tests and thus you can refactor more aggressively. Usually, this process is shorter than one would expect.