Java tool for testing private methods?

前端 未结 5 2051
忘了有多久
忘了有多久 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:06

    There are number of approaches to take

    • Don't test private methods as they are hidden implementation details which should never make a difference to the caller.
    • Make the methods package local so a caller cannot access them, but you can access them in the same package i.e. a unit test.
    • Make the unit test an inner class or provide a package local inner class. Not sure this is an improvement!
    • Use reflection to access the methods of the class. This is like marking a method rpivate when its not and is a confusion IMHO. You should be only marking a method private when it is truely private.

提交回复
热议问题