aspose file tools*
The moose likes Testing and the fly likes Refactorings and testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Refactorings and testing" Watch "Refactorings and testing" New topic
Author

Refactorings and testing

William Bernadsson
Greenhorn

Joined: Jul 14, 2008
Posts: 10
I am reading "Refactorings" book. It does strongly suggest to have unit test before doing any serious refactoring. But the refactoring may rearrange code, move functionality between methods and classes. As I understand unit test are unit based : class/methods. Then, the unit tests have to be refactored as well? How to keep unit tests match the tested code and do not introduce errors while refactoring?
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
Several things:

1) If your unit tests are testing the public interface of the class, and are focused on testing behavior rather than methods, the unit tests are somewhat resilient to many common refactorings (as they only change the internals of a class.

2) If your IDE offers built-in refactoring support, then when you use the built in refactorings, it'll take care of changing the test code when it has to.

3) If you've kept your test code refactored, even when you have to manually update test code to account for a new refactoring, you should only need to do it one place.
William Bernadsson
Greenhorn

Joined: Jul 14, 2008
Posts: 10
1) If your unit tests are testing the public interface of the class, and are focused on testing behavior rather than methods, the unit tests are somewhat resilient to many common refactorings (as they only change the internals of a class.


I have to refactor the code that lays out products, marketing images, and other widgets on shop page. It is quite complex - about of 1000 java lines of code and logic is fuzzy.
This is huge amount for testing, right? I can approach it 2 ways:
1. Test input against output. I can create big input data chunk, put it into XML and test it against the output. This way I will never have to change test code. On the other hand such testing would not really point to the place/method that is faulty. It just will say - does not work. That would be more black box testing.
2. If I write tests per method/classes I will have to refactor them as well. This is where I am afraid to introduce errors. But such tests would easier detect the place of the error and they can serve as some sort of example how the specific methods can be used.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
It is actually common practice to have *both* kinds of tests in place.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refactorings and testing