Search code examples
javaunit-testingpackageencapsulationaccess-specifier

How to do package-private classes visible for other one package only?


I want to implement package with optimal encapsulation, but test it. How can I do package-private members of one package visible for other one (friendly) package only?


Solution

  • Yes, it can be done. Kind of...

    package private stuff is visible to other classes in the same package, but not necessarily the same directory.

    You can declare a class as being in the same package but place it under another directory structure (eg your test code) or even within another project.

    You mentioned testing, so I assume you want to "see" this stuff in your tests. Just define your test classes as being in the same package (not the same directory) as your production code.