I have the following situation:
class Worker {
public Integer somework() {
Integer k=0;
Helper h= new Helper();
h.change(k);
return k;
}
}
class Helper {
public void change(Integer k) {
//k = Some calcs
}
}
I’m making unitests for Worker and obviously I want to mock Helper class so that his change method will always put 1 into k.
My real situation is more complicated but this code represents the issue. Thanks for help.
I would change the method signature and make it take a
Helperinstance as argument. The caller would create the helper and pass it to thesomeworkmethod. The test would pass a mock helper.If this isn’t possible, at least call a protected factory method to create the helper, and mock this factory method when testing the
someworkmethod in order to make it return a mock helper: