I seem to have hit an interesting road block recently. In trying to implement a high level part of my system, it finally came time to start calling methods of objects that are explicitly named, and I'm having a hard time figuring out how to test these. Let me give a short example.
First, I had the controller directly create the corresponding interactor. Not only did that break the dependency rule of the controller only knowing of the input port boundary, but I couldn't figure out how to test that, because in calling main on the interactor, too much work is being done.
public class Controller
{
public void doSomething()
{
Interactor interactor = new Interactor;
interactor.main();
}
}
That seemed like an easy fix. I'd could just dependency inject the interactor, which would fix both problems. Now the controller was testable, because I could mock the interactor and not have it do all of its normal work.
public class Controller
{
public void doSomething(InputPort interactor)
{
interactor.main();
}
}
Now, having the controller using proper abstractions, the same problem hit me again in the next layer up. I needed some way to call the controller associated with the current action/request, so I tried to use builder object to create the controller and give it the interactor it requires.
public class Builder
{
public void buildController
{
Controller controller = new Controller;
controller.doSomething(new Interactor);
}
}
And once again, I have the exact same problem. By naming the class explicitly and calling a method, I can't test
buildController because all heck breaks loose when
doSomething is called.
So I have two questions.
- How do I go about testing methods that call other, un-mockable methods?
- How do you guys call the controllers in your system in such a way that they don't know too much, and are testable?