Skip navigation.

JUnit testing private fields and methods

JUnit testing private fields and methods

Using Java reflection, you can access and even alter private and protected fields of any class. You can effectively break encapsulation with this in a very naughty way. I guess we shouldn't go about using this in actual applications, but for unit testing our own classes, it will come in quite handy, and the code is very simple, just a few lines.

Author: Arne Vandamme
Posted: August 31, 2004