A common question among developers is whether to test private methods. In principle, testers should test private methods only through their public methods. However, testers often feel the urge to test a particular private method in isolation.
A common reason for this feeling is the lack of cohesion or the complexity of the private method. In other words, this method does something so different from the public method, and/or its task is so complicated, that it must be tested separately. This is a good example of the test speaking to us. In terms of design, this may mean the private method does not belong in its current place. A common refactoring is to extract the method, perhaps to a brand new class. There, the former private method, now a public method, can be tested normally. The original class, where the private method used to be, should now depend on this new class.
Leave a Reply