Home > Net >  Can I make Java actively throw compile errors? (Yes.. compile errors...not exception)
Can I make Java actively throw compile errors? (Yes.. compile errors...not exception)

Time:10-08

There is a class I created that I only wish people to use in unit tests.

@VisibleForTesting is not ideal as it makes the class visible for testing in its own package only. While in my case this class I created is a util class that should be used for all unit tests from different packages. The reason we don't want to use it in production code is due to some complicated and legacy reasons...and we want to enforce it. In one word, enforce a class to be only usable in unit tests of different packages but not outside unit tests code

So I wonder what is the best solution? I think making Java throw compile errors if this class is used in non-test classes is a good idea... but I don't know how to do it, or if it is supported by Java at all

CodePudding user response:

For maven, define the class in the src/test/java source folder. That is only visible to test code.

For other build systems, have your tests and supporting classes in a separate project. You don’t want to ship test classes.

  • Related