Refactoring the rubbish into methods is fine. Now you can download Powermock to mock out all the terrible rubbish in the code. After you are done with mocking you can test your code.
It is even better if you refrain from adding anything to this monstrosity. You can compose your functionality into MonstrosityRubbish
(or whatever) by writing your own classes for the new stuff.
The main thing is not to touch any of the legacy code if possible, you can compose your code into it instead.
So in code:
private MyShinyClass yourShinyObject; // init
public void execute(Param param0) {
rubbish1();
yourShinyObject.handleError(param0);
rubbish2();
}
public class MyShinyClass {
public void handleError(Param param0) {
// your code here
}
}
If you can manage to write your new stuff like this you will only depend on Param
which can be mocked/stubbed and your code can be tested without setting your hair on fire.
It is even better if you can write it in a functional way so you don't have to manipulate responseProperties
in your separated code:
public void execute(Param param0) {
rubbish1();
responseProperties.add(yourShinyObject.fetchErrors(param0));
rubbish2();
}
public class MyShinyClass {
public List<HopelessError> fetchErrors(Param param0) {
// your code here
}
}
Of course execute()
does not need a parameter you can pass any variable/field you want to handleErrors()
.