Legacy Code To Testable Code #2: Extract Method

2 comments on “Legacy Code To Testable Code #2: Extract Method”

  1. rliesenfeld Reply

    I think you should show the Mockito test for the refactored method. It will have to use a spy in order to mock the extracted method, and use of spies (aka “partial mocking”) is not a recommended practice. In general, you want to mock a dependency of the class under test, and not the class under test itself. The article should be explicit about this downside.

  2. Gil Zilberfeld Reply

    Thank you for the suggestion!

    Like any technique, it’s about context. And it’s not about what I want, it’s about what I have. I may have a whole lot of freedom, and I can go to extract class. When I have constraints about code changes, I may choose to use partial mocking.

    The constraints are not in the code – this week I worked with a client who’s afraid to change other people’s code. His manager backs him up. I was allowed to extract a method on his class, but extracting to another class was not “acceptable”.

    Work within the limitations.

Leave A Reply

Your email address will not be published. Required fields are marked *