Confusion regarding unit testing of private methods

I have started writing unit testing for most of my android framework independent classes e.g: Repository, Usecase, etc. I want to know whether it is a good idea to use @VisibleForTesting to test private methods in a class?
@mmurphy

1 Like

Personally, I tend to use @VisibleForTesting only for cases where I cannot test the public API without it. I focus on black box testing, exercising the public API and confirming that it works.

That’s not to say that white-box testing (testing private APIs) is wrong. It just is not the approach that I personally use.

Even if your project has an objective of 100% code coverage, in principle all private APIs should be triggered by some use of public APIs — otherwise, that private API is unused and could be removed. However, it may be less painful to use @VisibleForTesting and write a more direct test than it would be to come up with tests that exercise the private API purely via the public API.

2 Likes

Yeah even I am using @VisibleForTesting most of the time. But some of my peers pointed out that it’s a code smell which I don’t agree. So wanted to get your suggestion on this part.