I am not able to write the junit for private method
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @djohn98390536,
then you should write an additional test case for your public method that covers the private method as well. Private methods serve no purpose if they are not used in any public methods. The best practice in unit and integration testing is to test the APIs you expose, therefore, your public methods.
Good luck,
Daniel
Please try PowerMockito.
It helps to write the Junit for Private Or Static.
Views
Replies
Total Likes
Hi @djohn98390536,
PowerMockito should not be used anymore as it is deprecated and not maintained. You shouldn't write any unit tests for private methods; instead, focus on writing tests for the public methods that internally use the private methods.
Good luck,
Daniel
Hi @daniel-strmecki while writing junit for public method this private method not getting covered and coverage is getting down. if u hv any example please share
Views
Replies
Total Likes
Hi @djohn98390536,
then you should write an additional test case for your public method that covers the private method as well. Private methods serve no purpose if they are not used in any public methods. The best practice in unit and integration testing is to test the APIs you expose, therefore, your public methods.
Good luck,
Daniel
Hi @djohn98390536 ,
Usually, if it's hard to cover logic in private methods it means that code requires refactoring, because of complexity, nesting, etc.
However, for example, if you test OSGi component that has interface and implementation (either it impl package or in internal), you can change private to package-private access modifier. It will allow you to write more tests for your method.
@djohn98390536 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Like
Replies