Stay Away From @VisibleForTesting

STOP

Best testing practices rely on focusing on code that is visible to external parties, whether that is another class, component, project, service, or product. If you find yourself wanting to test private methods or classes, rethink your design.

The annotation


@VisibleForTesting
 

exists for two purposes:

  1. Notify other developers that the method has been relaxed from private to package-private or protected to allow tests in the same package to access and test it. In other words: “Leave it the way it is, I meant to do this!”
  2. Instruct tools like Checkstyle to ignore methods when running the VisibilityModifierCheck. It is one of the default annotations to ignore for this check.

Thanks for reading!

Comments powered by Talkyard.