Unit testing private members in Java
04 Feb 2017Sometimes, you need to be able to look at the private members of your classes in order to test that something has gone to plan. Unit testing is one scenario where this makes sense.
By using the getDeclaredField
method, passing the name of the field; the reflection framework will send back the definition. This field gets executed through the use of the get
method, passing in the object instance.
To finish the picture here, we can also get access on methods that are private as well:
Done.