最终,我看到一条回复,颇有中庸的味道,基本上来说,不要针对私有方法写单元测试,如果需要写的话,先要说清楚,为什么要把这个方法设置成私有,而不是公有.最终例外的一些私有方法需要进行单元测试可以采用方法注入的方式,步骤摘抄如下
1) You create an inner class within the class under test - call it TestProbe
2) You create an accessor to that probe in the parent class - something like getTestProbe()
3) Because the inner class has access to all the private methods and variables, you can add as many getters/setters as you want to fudge with the inner state of the class under test.
4) You get to keep the parent class's original private variables and methods and minimally modify it's public interface by adding one getter: getTestProbe().
An example of a TestProbe might look something like this: