Writing/implementing an API: testability vs information hiding

后端 未结 6 1169
春和景丽
春和景丽 2021-01-21 12:56

Many times I am involved in the design/implementation of APIs I am facing this dilemma.

I am a very strong supporter of information hiding and try to use various techniq

6条回答
  •  时光说笑
    2021-01-21 13:06

    I don't see how information hiding, in the abstract, is reducing your testability.

    If you were injecting the SomethingThatExpectsMyInterface used in this method rather than constructing it directly:

    public void someMethod() {
       // calculate x, y and z
       SomethingThatExpectsMyInterface something = ...;
       something.submit(new InnerFoo(x, y, z));
    }
    

    Then in a unit test you could inject this class with a mock version of SomethingThatExpectsMyInterface and easily assert what happens when you call someMethod() with different inputs - that the mockSomething receives arguments of certain values.

    I think you may have over-simplified this example anyway as InnerFoo cannot be a private class if SomethingThatExpectsMyInterface receives arguments of its type.

    "Information Hiding" doesn't necessarily mean that the objects you pass between your classes need to be a secret - just that you aren't requiring external code using this class to be aware of the details of InnerFoo or the other details of how this class communicates with others.

提交回复
热议问题