public void myMethod(Long id) {
// ...
Dependency dependency = DependencyUtil.getDependency();
// ...
}
We can refactor this to:
public void myMethod(Long id) {
Dependency dependency = DependencyUtil.getDependency();
myMethod(dependency);
}
void myMethod(Long id, Dependency dependency) {
// ...
doSomething(dependency);
// ...
}
We overload the method signature and preserve the public API. We make the new method have default access to make it testable. We can now mock Dependency however we like.
No comments:
Post a Comment