I started to use JUnit and Mockito in my projects, what I have noticed quickly is I ended up converting my private methods to public in order to be accesed from test classes, which is a terrible solution.
Sometimes it is sufficient to test only the public methods, but sometimes i want to really test some internal methods as well. Is there a workaround for this? for example a special annotation that allows JUnit to mock a private method as public or something like that?
Your public methods must be calling your private methods.
So you can test your private method by making a test function of your public method.
If you still want to test private function by Junits, you can do it by using
reflection.You have to set the property of method
setAccessible==true.Example:
In the
getDeclaredMethodyou have to give the name of theprivatemethod , and the type of theargumentsyou are passing in the function in order.