Java tool for testing private methods?

前端 未结 5 2003
忘了有多久
忘了有多久 2021-02-20 10:32

There are different opinions on the meaningfulness of testing of private methods, e.g., here and here. I personally think it makes sense, the question is how to do it properly.

5条回答
  •  滥情空心
    2021-02-20 11:17

    I worked on a project a few years back that generated classes to make it easier to unit test private methods. http://java.net/projects/privateer/

    It generated extra classes that made it easier than calling reflection, e.g. if you had MyClass.myPrivateMethod() it would generate a _MyClass class that would allow invocation of myPrivateMethod directly.

    It was never really finished and was kind of useful for a few cases, but overall I wouldn't recommend testing private methods unless absolutely necessary. Usually redesigning them into utility classes (with package access if you're worried about users using them) is a better option.

提交回复
热议问题