Extract and Override – code cast
Blog post series
This blog post is part of a series about legacy coderetreat and legacy code techniques you can apply during your work. Please click to see more sessions about legacy code.
Code Cast
This is a code cast in Java.
Extract and Override is a very useful technique to break static dependencies. Whenever we cannot write tests for a piece of code, it is often because we have static dependencies. In the code cast below you can see how to use extract and override having a random number generator very tightly coupled. Because of that, we cannot have the same output for some clear steps. The solution found in this video is to extract the random to a new method and then override the random generator, just for tests. In this way we have predictability for tests.
Read here more about this concept in my blog post.